]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
af6f0d42 | 2 | |
01234e1f | 3 | #include <linux/netdevice.h> |
43b3a5ef TG |
4 | #include <netinet/ether.h> |
5 | ||
e5eadf53 | 6 | #include "sd-device.h" |
07630cea | 7 | #include "sd-netlink.h" |
af6f0d42 | 8 | |
b5efdb8a | 9 | #include "alloc-util.h" |
07630cea LP |
10 | #include "conf-files.h" |
11 | #include "conf-parser.h" | |
dc0d4078 | 12 | #include "def.h" |
b220632c | 13 | #include "device-util.h" |
a5010333 | 14 | #include "ethtool-util.h" |
3ffd4af2 | 15 | #include "fd-util.h" |
07630cea | 16 | #include "link-config.h" |
af6f0d42 | 17 | #include "log.h" |
0a970718 | 18 | #include "memory-util.h" |
73d2bb08 | 19 | #include "naming-scheme.h" |
1c4baffc | 20 | #include "netlink-util.h" |
c6f7c917 | 21 | #include "network-internal.h" |
6bedfcbb | 22 | #include "parse-util.h" |
07630cea | 23 | #include "path-util.h" |
4e731273 | 24 | #include "proc-cmdline.h" |
3df3e884 | 25 | #include "random-util.h" |
8fcde012 | 26 | #include "stat-util.h" |
8b43440b | 27 | #include "string-table.h" |
07630cea LP |
28 | #include "string-util.h" |
29 | #include "strv.h" | |
af6f0d42 TG |
30 | |
31 | struct link_config_ctx { | |
32 | LIST_HEAD(link_config, links); | |
33 | ||
a5010333 TG |
34 | int ethtool_fd; |
35 | ||
f6194225 TG |
36 | bool enable_name_policy; |
37 | ||
1c4baffc | 38 | sd_netlink *rtnl; |
43b3a5ef | 39 | |
dc0d4078 | 40 | usec_t network_dirs_ts_usec; |
af6f0d42 TG |
41 | }; |
42 | ||
9a4b012e TG |
43 | static void link_config_free(link_config *link) { |
44 | if (!link) | |
45 | return; | |
5b9d4dc0 | 46 | |
9a4b012e TG |
47 | free(link->filename); |
48 | ||
e90d0374 | 49 | set_free_free(link->match_mac); |
43d60b77 TG |
50 | strv_free(link->match_path); |
51 | strv_free(link->match_driver); | |
52 | strv_free(link->match_type); | |
391f6bc1 | 53 | strv_free(link->match_name); |
44005bfb | 54 | strv_free(link->match_property); |
c4f58dea | 55 | condition_free_list(link->conditions); |
9a4b012e TG |
56 | |
57 | free(link->description); | |
58 | free(link->mac); | |
59 | free(link->name_policy); | |
60 | free(link->name); | |
a5053a15 | 61 | strv_free(link->alternative_names); |
ef1d2c07 | 62 | free(link->alternative_names_policy); |
9a4b012e TG |
63 | free(link->alias); |
64 | ||
65 | free(link); | |
af6f0d42 TG |
66 | } |
67 | ||
9a4b012e TG |
68 | DEFINE_TRIVIAL_CLEANUP_FUNC(link_config*, link_config_free); |
69 | ||
af6f0d42 TG |
70 | static void link_configs_free(link_config_ctx *ctx) { |
71 | link_config *link, *link_next; | |
72 | ||
73 | if (!ctx) | |
74 | return; | |
75 | ||
9a4b012e TG |
76 | LIST_FOREACH_SAFE(links, link, link_next, ctx->links) |
77 | link_config_free(link); | |
af6f0d42 TG |
78 | } |
79 | ||
80 | void link_config_ctx_free(link_config_ctx *ctx) { | |
81 | if (!ctx) | |
82 | return; | |
83 | ||
03e334a1 | 84 | safe_close(ctx->ethtool_fd); |
43b3a5ef | 85 | |
1c4baffc | 86 | sd_netlink_unref(ctx->rtnl); |
43b3a5ef | 87 | |
af6f0d42 TG |
88 | link_configs_free(ctx); |
89 | ||
90 | free(ctx); | |
91 | ||
92 | return; | |
93 | } | |
94 | ||
9a4b012e TG |
95 | int link_config_ctx_new(link_config_ctx **ret) { |
96 | _cleanup_(link_config_ctx_freep) link_config_ctx *ctx = NULL; | |
97 | ||
98 | if (!ret) | |
99 | return -EINVAL; | |
100 | ||
101 | ctx = new0(link_config_ctx, 1); | |
102 | if (!ctx) | |
103 | return -ENOMEM; | |
104 | ||
105 | LIST_HEAD_INIT(ctx->links); | |
106 | ||
107 | ctx->ethtool_fd = -1; | |
108 | ||
109 | ctx->enable_name_policy = true; | |
110 | ||
1cc6c93a | 111 | *ret = TAKE_PTR(ctx); |
9a4b012e TG |
112 | |
113 | return 0; | |
114 | } | |
115 | ||
a378400b | 116 | int link_load_one(link_config_ctx *ctx, const char *filename) { |
9a4b012e | 117 | _cleanup_(link_config_freep) link_config *link = NULL; |
6e37cd2f | 118 | _cleanup_fclose_ FILE *file = NULL; |
6cdab9f1 | 119 | _cleanup_free_ char *name = NULL; |
79a60834 | 120 | size_t i; |
af6f0d42 TG |
121 | int r; |
122 | ||
187dc6e5 TA |
123 | assert(ctx); |
124 | assert(filename); | |
125 | ||
af6f0d42 | 126 | file = fopen(filename, "re"); |
f2d251cd YW |
127 | if (!file) |
128 | return errno == ENOENT ? 0 : -errno; | |
af6f0d42 | 129 | |
ed88bcfb ZJS |
130 | if (null_or_empty_fd(fileno(file))) { |
131 | log_debug("Skipping empty file: %s", filename); | |
132 | return 0; | |
133 | } | |
134 | ||
6cdab9f1 YW |
135 | name = strdup(filename); |
136 | if (!name) | |
137 | return -ENOMEM; | |
138 | ||
139 | link = new(link_config, 1); | |
ecb08ec6 | 140 | if (!link) |
e8a42907 | 141 | return -ENOMEM; |
af6f0d42 | 142 | |
6cdab9f1 YW |
143 | *link = (link_config) { |
144 | .filename = TAKE_PTR(name), | |
54ed9f88 | 145 | .mac_address_policy = _MAC_ADDRESS_POLICY_INVALID, |
6cdab9f1 YW |
146 | .wol = _WOL_INVALID, |
147 | .duplex = _DUP_INVALID, | |
148 | .port = _NET_DEV_PORT_INVALID, | |
149 | .autonegotiation = -1, | |
150 | }; | |
5fde13d7 | 151 | |
79a60834 | 152 | for (i = 0; i < ELEMENTSOF(link->features); i++) |
cc2ff878 | 153 | link->features[i] = -1; |
50725d10 | 154 | |
e9f3d2d5 | 155 | r = config_parse(NULL, filename, file, |
c6b3370a | 156 | "Match\0Link\0", |
e9f3d2d5 | 157 | config_item_perf_lookup, link_config_gperf_lookup, |
bcde742e | 158 | CONFIG_PARSE_WARN, link); |
36f822c4 | 159 | if (r < 0) |
ecb08ec6 | 160 | return r; |
af6f0d42 | 161 | |
4e964aa0 | 162 | if (link->speed > UINT_MAX) |
dab495dc TG |
163 | return -ERANGE; |
164 | ||
84ea567e YW |
165 | if (set_isempty(link->match_mac) && strv_isempty(link->match_path) && |
166 | strv_isempty(link->match_driver) && strv_isempty(link->match_type) && | |
44005bfb | 167 | strv_isempty(link->match_name) && strv_isempty(link->match_property) && !link->conditions) |
84ea567e YW |
168 | log_warning("%s: No valid settings found in the [Match] section. " |
169 | "The file will match all interfaces. " | |
170 | "If that is intended, please add OriginalName=* in the [Match] section.", | |
171 | filename); | |
172 | ||
c4f58dea | 173 | if (!condition_test_list(link->conditions, NULL, NULL, NULL)) { |
176d9c0e YW |
174 | log_debug("%s: Conditions do not match the system environment, skipping.", filename); |
175 | return 0; | |
176 | } | |
177 | ||
6cdab9f1 | 178 | log_debug("Parsed configuration file %s", filename); |
af6f0d42 | 179 | |
4b4a6c9b | 180 | LIST_PREPEND(links, ctx->links, TAKE_PTR(link)); |
af6f0d42 | 181 | return 0; |
af6f0d42 TG |
182 | } |
183 | ||
f6194225 | 184 | static bool enable_name_policy(void) { |
1d84ad94 | 185 | bool b; |
f6194225 | 186 | |
1d84ad94 | 187 | return proc_cmdline_get_bool("net.ifnames", &b) <= 0 || b; |
f6194225 TG |
188 | } |
189 | ||
015b097c | 190 | static int link_unsigned_attribute(sd_device *device, const char *attr, unsigned *type) { |
0b189e8f ZJS |
191 | const char *s; |
192 | int r; | |
193 | ||
015b097c | 194 | r = sd_device_get_sysattr_value(device, attr, &s); |
0b189e8f | 195 | if (r < 0) |
015b097c | 196 | return log_device_debug_errno(device, r, "Failed to query %s: %m", attr); |
0b189e8f ZJS |
197 | |
198 | r = safe_atou(s, type); | |
199 | if (r < 0) | |
015b097c | 200 | return log_device_warning_errno(device, r, "Failed to parse %s \"%s\": %m", attr, s); |
0b189e8f | 201 | |
015b097c | 202 | log_device_debug(device, "Device has %s=%u", attr, *type); |
0b189e8f ZJS |
203 | return 0; |
204 | } | |
205 | ||
af6f0d42 | 206 | int link_config_load(link_config_ctx *ctx) { |
edf029b7 TG |
207 | _cleanup_strv_free_ char **files; |
208 | char **f; | |
a39f92d3 | 209 | int r; |
af6f0d42 TG |
210 | |
211 | link_configs_free(ctx); | |
212 | ||
f6194225 TG |
213 | if (!enable_name_policy()) { |
214 | ctx->enable_name_policy = false; | |
3f85ef0f | 215 | log_info("Network interface NamePolicy= disabled on kernel command line, ignoring."); |
f6194225 TG |
216 | } |
217 | ||
97f2d76d | 218 | /* update timestamp */ |
dc0d4078 | 219 | paths_check_timestamp(NETWORK_DIRS, &ctx->network_dirs_ts_usec, true); |
af6f0d42 | 220 | |
dc0d4078 | 221 | r = conf_files_list_strv(&files, ".link", NULL, 0, NETWORK_DIRS); |
f647962d MS |
222 | if (r < 0) |
223 | return log_error_errno(r, "failed to enumerate link files: %m"); | |
af6f0d42 TG |
224 | |
225 | STRV_FOREACH_BACKWARDS(f, files) { | |
a378400b | 226 | r = link_load_one(ctx, *f); |
af6f0d42 | 227 | if (r < 0) |
e8a42907 | 228 | log_error_errno(r, "Failed to load %s, ignoring: %m", *f); |
af6f0d42 TG |
229 | } |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
234 | bool link_config_should_reload(link_config_ctx *ctx) { | |
dc0d4078 | 235 | return paths_check_timestamp(NETWORK_DIRS, &ctx->network_dirs_ts_usec, false); |
af6f0d42 TG |
236 | } |
237 | ||
e5eadf53 | 238 | int link_config_get(link_config_ctx *ctx, sd_device *device, link_config **ret) { |
af6f0d42 TG |
239 | link_config *link; |
240 | ||
3b64e4d4 TG |
241 | assert(ctx); |
242 | assert(device); | |
243 | assert(ret); | |
244 | ||
af6f0d42 | 245 | LIST_FOREACH(links, link, ctx->links) { |
edbb03e9 | 246 | if (net_match_config(link->match_mac, link->match_path, link->match_driver, |
78404d22 | 247 | link->match_type, link->match_name, link->match_property, NULL, NULL, NULL, |
572b21d9 | 248 | device, NULL, NULL, NULL, 0, NULL, NULL)) { |
56637e5c | 249 | if (link->match_name && !strv_contains(link->match_name, "*")) { |
015b097c | 250 | unsigned name_assign_type = NET_NAME_UNKNOWN; |
32bc8adc | 251 | |
015b097c | 252 | (void) link_unsigned_attribute(device, "name_assign_type", &name_assign_type); |
32bc8adc | 253 | |
56637e5c | 254 | if (name_assign_type == NET_NAME_ENUM) { |
b38de0e9 YW |
255 | log_device_warning(device, "Config file %s applies to device based on potentially unpredictable interface name", |
256 | link->filename); | |
32bc8adc TG |
257 | *ret = link; |
258 | ||
259 | return 0; | |
260 | } else if (name_assign_type == NET_NAME_RENAMED) { | |
b38de0e9 YW |
261 | log_device_warning(device, "Config file %s matches device based on renamed interface name, ignoring", |
262 | link->filename); | |
32bc8adc | 263 | |
ca6038b8 | 264 | continue; |
32bc8adc | 265 | } |
ca6038b8 | 266 | } |
32bc8adc | 267 | |
b38de0e9 | 268 | log_device_debug(device, "Config file %s is applied", link->filename); |
32bc8adc | 269 | |
ca6038b8 | 270 | *ret = link; |
ca6038b8 | 271 | return 0; |
af6f0d42 TG |
272 | } |
273 | } | |
274 | ||
be32eb9b | 275 | *ret = NULL; |
af6f0d42 TG |
276 | return -ENOENT; |
277 | } | |
278 | ||
54ed9f88 | 279 | static int get_mac(sd_device *device, MACAddressPolicy policy, struct ether_addr *mac) { |
015b097c | 280 | unsigned addr_type; |
54ed9f88 | 281 | bool want_random = policy == MAC_ADDRESS_POLICY_RANDOM; |
f1ac7002 | 282 | int r; |
16b9b87a | 283 | |
54ed9f88 | 284 | assert(IN_SET(policy, MAC_ADDRESS_POLICY_RANDOM, MAC_ADDRESS_POLICY_PERSISTENT)); |
3c9b8860 | 285 | |
015b097c | 286 | r = link_unsigned_attribute(device, "addr_assign_type", &addr_type); |
f1ac7002 | 287 | if (r < 0) |
015b097c ZJS |
288 | return r; |
289 | switch (addr_type) { | |
290 | case NET_ADDR_SET: | |
291 | return log_device_debug(device, "MAC on the device already set by userspace"); | |
292 | case NET_ADDR_STOLEN: | |
293 | return log_device_debug(device, "MAC on the device already set based on another device"); | |
294 | case NET_ADDR_RANDOM: | |
295 | case NET_ADDR_PERM: | |
296 | break; | |
297 | default: | |
298 | return log_device_warning(device, "Unknown addr_assign_type %u, ignoring", addr_type); | |
299 | } | |
04b67d49 | 300 | |
015b097c ZJS |
301 | if (want_random == (addr_type == NET_ADDR_RANDOM)) |
302 | return log_device_debug(device, "MAC on the device already matches policy *%s*", | |
54ed9f88 | 303 | mac_address_policy_to_string(policy)); |
16b9b87a | 304 | |
015b097c ZJS |
305 | if (want_random) { |
306 | log_device_debug(device, "Using random bytes to generate MAC"); | |
9bf3b535 | 307 | random_bytes(mac->ether_addr_octet, ETH_ALEN); |
015b097c | 308 | } else { |
dbe81cbd | 309 | uint64_t result; |
9bf3b535 | 310 | |
96848152 ZJS |
311 | r = net_get_unique_predictable_data(device, |
312 | naming_scheme_has(NAMING_STABLE_VIRTUAL_MACS), | |
313 | &result); | |
16b9b87a | 314 | if (r < 0) |
015b097c | 315 | return log_device_warning_errno(device, r, "Could not generate persistent MAC: %m"); |
16b9b87a | 316 | |
96848152 | 317 | log_device_debug(device, "Using generated persistent MAC address"); |
9bf3b535 | 318 | assert_cc(ETH_ALEN <= sizeof(result)); |
dbe81cbd | 319 | memcpy(mac->ether_addr_octet, &result, ETH_ALEN); |
16b9b87a TG |
320 | } |
321 | ||
322 | /* see eth_random_addr in the kernel */ | |
3c9b8860 TG |
323 | mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */ |
324 | mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */ | |
015b097c | 325 | return 1; |
16b9b87a TG |
326 | } |
327 | ||
464cf22f | 328 | int link_config_apply(link_config_ctx *ctx, link_config *config, |
e5eadf53 | 329 | sd_device *device, const char **name) { |
ef1d2c07 | 330 | _cleanup_strv_free_ char **altnames = NULL; |
5fde13d7 | 331 | struct ether_addr generated_mac; |
16b9b87a | 332 | struct ether_addr *mac = NULL; |
a39f92d3 SS |
333 | const char *new_name = NULL; |
334 | const char *old_name; | |
0b189e8f ZJS |
335 | unsigned speed, name_type = NET_NAME_UNKNOWN; |
336 | NamePolicy policy; | |
43b3a5ef | 337 | int r, ifindex; |
af6f0d42 | 338 | |
3e137a1b TG |
339 | assert(ctx); |
340 | assert(config); | |
341 | assert(device); | |
342 | assert(name); | |
343 | ||
e5eadf53 YW |
344 | r = sd_device_get_sysname(device, &old_name); |
345 | if (r < 0) | |
346 | return r; | |
af6f0d42 | 347 | |
5c2316c6 YW |
348 | r = ethtool_set_glinksettings(&ctx->ethtool_fd, old_name, |
349 | config->autonegotiation, config->advertise, | |
350 | config->speed, config->duplex, config->port); | |
a39f92d3 SS |
351 | if (r < 0) { |
352 | ||
bb79318e | 353 | if (config->port != _NET_DEV_PORT_INVALID) |
49c603bd | 354 | log_warning_errno(r, "Could not set port (%s) of %s: %m", port_to_string(config->port), old_name); |
bb79318e | 355 | |
5dd10118 | 356 | if (!eqzero(config->advertise)) |
2d18ac44 | 357 | log_warning_errno(r, "Could not set advertise mode: %m"); /* TODO: include modes in the log message. */ |
a39f92d3 | 358 | |
6cf0a204 | 359 | if (config->speed) { |
6cf0a204 SS |
360 | speed = DIV_ROUND_UP(config->speed, 1000000); |
361 | if (r == -EOPNOTSUPP) { | |
362 | r = ethtool_set_speed(&ctx->ethtool_fd, old_name, speed, config->duplex); | |
363 | if (r < 0) | |
364 | log_warning_errno(r, "Could not set speed of %s to %u Mbps: %m", old_name, speed); | |
365 | } | |
366 | } | |
367 | ||
6eee8857 YW |
368 | if (config->duplex != _DUP_INVALID) |
369 | log_warning_errno(r, "Could not set duplex of %s to %s: %m", old_name, duplex_to_string(config->duplex)); | |
a39f92d3 | 370 | } |
a5010333 | 371 | |
aedca892 | 372 | r = ethtool_set_wol(&ctx->ethtool_fd, old_name, config->wol); |
5fde13d7 | 373 | if (r < 0) |
755bde37 LP |
374 | log_warning_errno(r, "Could not set WakeOnLan of %s to %s: %m", |
375 | old_name, wol_to_string(config->wol)); | |
af6f0d42 | 376 | |
50725d10 SS |
377 | r = ethtool_set_features(&ctx->ethtool_fd, old_name, config->features); |
378 | if (r < 0) | |
379 | log_warning_errno(r, "Could not set offload features of %s: %m", old_name); | |
380 | ||
5f945202 SS |
381 | if (config->channels.rx_count_set || config->channels.tx_count_set || config->channels.other_count_set || config->channels.combined_count_set) { |
382 | r = ethtool_set_channels(&ctx->ethtool_fd, old_name, &config->channels); | |
383 | if (r < 0) | |
384 | log_warning_errno(r, "Could not set channels of %s: %m", old_name); | |
385 | } | |
386 | ||
224ded67 SS |
387 | if (config->ring.rx_pending_set || config->ring.tx_pending_set) { |
388 | r = ethtool_set_nic_buffer_size(&ctx->ethtool_fd, old_name, &config->ring); | |
389 | if (r < 0) | |
390 | log_warning_errno(r, "Could not set ring buffer of %s: %m", old_name); | |
391 | } | |
392 | ||
e5eadf53 YW |
393 | r = sd_device_get_ifindex(device, &ifindex); |
394 | if (r < 0) | |
b220632c | 395 | return log_device_warning_errno(device, r, "Could not find ifindex: %m"); |
43b3a5ef | 396 | |
015b097c | 397 | (void) link_unsigned_attribute(device, "name_assign_type", &name_type); |
0b189e8f | 398 | |
73d2bb08 ZJS |
399 | if (IN_SET(name_type, NET_NAME_USER, NET_NAME_RENAMED) |
400 | && !naming_scheme_has(NAMING_ALLOW_RERENAMES)) { | |
401 | log_device_debug(device, "Device already has a name given by userspace, not renaming."); | |
402 | goto no_rename; | |
403 | } | |
404 | ||
0b189e8f | 405 | if (ctx->enable_name_policy && config->name_policy) |
78f8849f | 406 | for (NamePolicy *p = config->name_policy; *p != _NAMEPOLICY_INVALID; p++) { |
0b189e8f ZJS |
407 | policy = *p; |
408 | ||
409 | switch (policy) { | |
410 | case NAMEPOLICY_KERNEL: | |
411 | if (name_type != NET_NAME_PREDICTABLE) | |
412 | continue; | |
413 | ||
414 | /* The kernel claims to have given a predictable name, keep it. */ | |
415 | log_device_debug(device, "Policy *%s*: keeping predictable kernel name", | |
416 | name_policy_to_string(policy)); | |
417 | goto no_rename; | |
3907446f ZJS |
418 | case NAMEPOLICY_KEEP: |
419 | if (!IN_SET(name_type, NET_NAME_USER, NET_NAME_RENAMED)) | |
420 | continue; | |
421 | ||
422 | log_device_debug(device, "Policy *%s*: keeping existing userspace name", | |
423 | name_policy_to_string(policy)); | |
424 | goto no_rename; | |
0b189e8f ZJS |
425 | case NAMEPOLICY_DATABASE: |
426 | (void) sd_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE", &new_name); | |
427 | break; | |
428 | case NAMEPOLICY_ONBOARD: | |
429 | (void) sd_device_get_property_value(device, "ID_NET_NAME_ONBOARD", &new_name); | |
430 | break; | |
431 | case NAMEPOLICY_SLOT: | |
432 | (void) sd_device_get_property_value(device, "ID_NET_NAME_SLOT", &new_name); | |
433 | break; | |
434 | case NAMEPOLICY_PATH: | |
435 | (void) sd_device_get_property_value(device, "ID_NET_NAME_PATH", &new_name); | |
436 | break; | |
437 | case NAMEPOLICY_MAC: | |
438 | (void) sd_device_get_property_value(device, "ID_NET_NAME_MAC", &new_name); | |
439 | break; | |
440 | default: | |
441 | assert_not_reached("invalid policy"); | |
5fde13d7 | 442 | } |
78f8849f YW |
443 | if (ifname_valid(new_name)) |
444 | break; | |
daeb71a3 | 445 | } |
daeb71a3 | 446 | |
0b189e8f ZJS |
447 | if (new_name) |
448 | log_device_debug(device, "Policy *%s* yields \"%s\".", name_policy_to_string(policy), new_name); | |
449 | else if (config->name) { | |
450 | new_name = config->name; | |
451 | log_device_debug(device, "Policies didn't yield a name, using specified Name=%s.", new_name); | |
ed308023 | 452 | } else |
0b189e8f ZJS |
453 | log_device_debug(device, "Policies didn't yield a name and Name= is not given, not renaming."); |
454 | no_rename: | |
04b67d49 | 455 | |
54ed9f88 ZJS |
456 | if (IN_SET(config->mac_address_policy, MAC_ADDRESS_POLICY_PERSISTENT, MAC_ADDRESS_POLICY_RANDOM)) { |
457 | if (get_mac(device, config->mac_address_policy, &generated_mac) > 0) | |
015b097c ZJS |
458 | mac = &generated_mac; |
459 | } else | |
460 | mac = config->mac; | |
16b9b87a | 461 | |
dab495dc | 462 | r = rtnl_set_link_properties(&ctx->rtnl, ifindex, config->alias, mac, config->mtu); |
f647962d | 463 | if (r < 0) |
7b72fe21 | 464 | return log_warning_errno(r, "Could not set Alias=, MACAddress= or MTU= on %s: %m", old_name); |
43b3a5ef | 465 | |
ef1d2c07 YW |
466 | if (config->alternative_names) { |
467 | altnames = strv_copy(config->alternative_names); | |
468 | if (!altnames) | |
469 | return log_oom(); | |
470 | } | |
471 | ||
472 | if (config->alternative_names_policy) | |
473 | for (NamePolicy *p = config->alternative_names_policy; *p != _NAMEPOLICY_INVALID; p++) { | |
474 | const char *n; | |
475 | ||
476 | switch (*p) { | |
477 | case NAMEPOLICY_DATABASE: | |
478 | (void) sd_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE", &n); | |
479 | break; | |
480 | case NAMEPOLICY_ONBOARD: | |
481 | (void) sd_device_get_property_value(device, "ID_NET_NAME_ONBOARD", &n); | |
482 | break; | |
483 | case NAMEPOLICY_SLOT: | |
484 | (void) sd_device_get_property_value(device, "ID_NET_NAME_SLOT", &n); | |
485 | break; | |
486 | case NAMEPOLICY_PATH: | |
487 | (void) sd_device_get_property_value(device, "ID_NET_NAME_PATH", &n); | |
488 | break; | |
489 | case NAMEPOLICY_MAC: | |
490 | (void) sd_device_get_property_value(device, "ID_NET_NAME_MAC", &n); | |
491 | break; | |
492 | default: | |
493 | assert_not_reached("invalid policy"); | |
494 | } | |
495 | if (!isempty(n)) { | |
496 | r = strv_extend(&altnames, n); | |
497 | if (r < 0) | |
498 | return log_oom(); | |
499 | } | |
500 | } | |
501 | ||
502 | if (new_name) | |
503 | strv_remove(altnames, new_name); | |
504 | strv_remove(altnames, old_name); | |
505 | strv_uniq(altnames); | |
4d016e96 | 506 | strv_sort(altnames); |
ef1d2c07 YW |
507 | |
508 | r = rtnl_set_link_alternative_names(&ctx->rtnl, ifindex, altnames); | |
bb181dd4 | 509 | if (r == -EOPNOTSUPP) |
ef1d2c07 | 510 | log_debug_errno(r, "Could not set AlternativeName= or apply AlternativeNamesPolicy= on %s, ignoring: %m", old_name); |
bb181dd4 | 511 | else if (r < 0) |
ef1d2c07 | 512 | return log_warning_errno(r, "Could not set AlternativeName= or apply AlternativeNamesPolicy= on %s: %m", old_name); |
a5053a15 | 513 | |
d95b83b8 TG |
514 | *name = new_name; |
515 | ||
af6f0d42 TG |
516 | return 0; |
517 | } | |
be32eb9b | 518 | |
e5eadf53 | 519 | int link_get_driver(link_config_ctx *ctx, sd_device *device, char **ret) { |
847a8a5f | 520 | const char *name; |
a7f7d1bd | 521 | char *driver = NULL; |
847a8a5f TG |
522 | int r; |
523 | ||
e5eadf53 YW |
524 | r = sd_device_get_sysname(device, &name); |
525 | if (r < 0) | |
526 | return r; | |
847a8a5f | 527 | |
aedca892 | 528 | r = ethtool_get_driver(&ctx->ethtool_fd, name, &driver); |
847a8a5f TG |
529 | if (r < 0) |
530 | return r; | |
531 | ||
532 | *ret = driver; | |
533 | return 0; | |
534 | } | |
535 | ||
54ed9f88 ZJS |
536 | static const char* const mac_address_policy_table[_MAC_ADDRESS_POLICY_MAX] = { |
537 | [MAC_ADDRESS_POLICY_PERSISTENT] = "persistent", | |
538 | [MAC_ADDRESS_POLICY_RANDOM] = "random", | |
539 | [MAC_ADDRESS_POLICY_NONE] = "none", | |
be32eb9b TG |
540 | }; |
541 | ||
54ed9f88 ZJS |
542 | DEFINE_STRING_TABLE_LOOKUP(mac_address_policy, MACAddressPolicy); |
543 | DEFINE_CONFIG_PARSE_ENUM(config_parse_mac_address_policy, mac_address_policy, MACAddressPolicy, | |
464cf22f | 544 | "Failed to parse MAC address policy"); |
be32eb9b | 545 | |
2c5859af | 546 | static const char* const name_policy_table[_NAMEPOLICY_MAX] = { |
04b67d49 | 547 | [NAMEPOLICY_KERNEL] = "kernel", |
3907446f | 548 | [NAMEPOLICY_KEEP] = "keep", |
e51660ae | 549 | [NAMEPOLICY_DATABASE] = "database", |
be32eb9b TG |
550 | [NAMEPOLICY_ONBOARD] = "onboard", |
551 | [NAMEPOLICY_SLOT] = "slot", | |
552 | [NAMEPOLICY_PATH] = "path", | |
3907446f | 553 | [NAMEPOLICY_MAC] = "mac", |
be32eb9b TG |
554 | }; |
555 | ||
556 | DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy); | |
464cf22f TG |
557 | DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy, |
558 | _NAMEPOLICY_INVALID, | |
559 | "Failed to parse interface name policy"); | |
ef1d2c07 YW |
560 | |
561 | static const char* const alternative_names_policy_table[_NAMEPOLICY_MAX] = { | |
562 | [NAMEPOLICY_DATABASE] = "database", | |
563 | [NAMEPOLICY_ONBOARD] = "onboard", | |
564 | [NAMEPOLICY_SLOT] = "slot", | |
565 | [NAMEPOLICY_PATH] = "path", | |
566 | [NAMEPOLICY_MAC] = "mac", | |
567 | }; | |
568 | ||
569 | DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy, NamePolicy); | |
570 | DEFINE_CONFIG_PARSE_ENUMV(config_parse_alternative_names_policy, alternative_names_policy, NamePolicy, | |
571 | _NAMEPOLICY_INVALID, | |
572 | "Failed to parse alternative names policy"); |