]>
Commit | Line | Data |
---|---|---|
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" |
9e2b7763 | 11 | #include "arphrd-util.h" |
07630cea LP |
12 | #include "conf-files.h" |
13 | #include "conf-parser.h" | |
d3867133 | 14 | #include "creds-util.h" |
dc0d4078 | 15 | #include "def.h" |
e0e789c1 | 16 | #include "device-private.h" |
b220632c | 17 | #include "device-util.h" |
a5010333 | 18 | #include "ethtool-util.h" |
3ffd4af2 | 19 | #include "fd-util.h" |
d3867133 | 20 | #include "fileio.h" |
07630cea | 21 | #include "link-config.h" |
613701a4 | 22 | #include "log-link.h" |
0a970718 | 23 | #include "memory-util.h" |
7e19cc54 | 24 | #include "net-condition.h" |
bd29dfef | 25 | #include "netif-sriov.h" |
b5cc5591 | 26 | #include "netif-util.h" |
1c4baffc | 27 | #include "netlink-util.h" |
6bedfcbb | 28 | #include "parse-util.h" |
b0c82192 | 29 | #include "path-lookup.h" |
07630cea | 30 | #include "path-util.h" |
4e731273 | 31 | #include "proc-cmdline.h" |
3df3e884 | 32 | #include "random-util.h" |
8fcde012 | 33 | #include "stat-util.h" |
8b43440b | 34 | #include "string-table.h" |
07630cea LP |
35 | #include "string-util.h" |
36 | #include "strv.h" | |
fc27088a | 37 | #include "utf8.h" |
af6f0d42 | 38 | |
afca7ac1 | 39 | struct LinkConfigContext { |
418c02c3 | 40 | LIST_HEAD(LinkConfig, configs); |
a5010333 | 41 | int ethtool_fd; |
f6194225 | 42 | bool enable_name_policy; |
dc0d4078 | 43 | usec_t network_dirs_ts_usec; |
af6f0d42 TG |
44 | }; |
45 | ||
418c02c3 YW |
46 | static LinkConfig* link_config_free(LinkConfig *config) { |
47 | if (!config) | |
75db809a | 48 | return NULL; |
5b9d4dc0 | 49 | |
418c02c3 | 50 | free(config->filename); |
9a4b012e | 51 | |
418c02c3 YW |
52 | net_match_clear(&config->match); |
53 | condition_free_list(config->conditions); | |
9a4b012e | 54 | |
418c02c3 | 55 | free(config->description); |
418c02c3 YW |
56 | free(config->name_policy); |
57 | free(config->name); | |
58 | strv_free(config->alternative_names); | |
59 | free(config->alternative_names_policy); | |
60 | free(config->alias); | |
61 | free(config->wol_password_file); | |
62 | erase_and_free(config->wol_password); | |
9a4b012e | 63 | |
bd29dfef YW |
64 | ordered_hashmap_free_with_destructor(config->sr_iov_by_section, sr_iov_free); |
65 | ||
418c02c3 | 66 | return mfree(config); |
af6f0d42 TG |
67 | } |
68 | ||
ce01c07f | 69 | DEFINE_TRIVIAL_CLEANUP_FUNC(LinkConfig*, link_config_free); |
9a4b012e | 70 | |
afca7ac1 | 71 | static void link_configs_free(LinkConfigContext *ctx) { |
af6f0d42 TG |
72 | if (!ctx) |
73 | return; | |
74 | ||
80a226b2 | 75 | LIST_FOREACH(configs, config, ctx->configs) |
418c02c3 | 76 | link_config_free(config); |
af6f0d42 TG |
77 | } |
78 | ||
afca7ac1 | 79 | LinkConfigContext *link_config_ctx_free(LinkConfigContext *ctx) { |
af6f0d42 | 80 | if (!ctx) |
75db809a | 81 | return NULL; |
af6f0d42 | 82 | |
03e334a1 | 83 | safe_close(ctx->ethtool_fd); |
af6f0d42 | 84 | link_configs_free(ctx); |
75db809a | 85 | return mfree(ctx); |
af6f0d42 TG |
86 | } |
87 | ||
afca7ac1 YW |
88 | int link_config_ctx_new(LinkConfigContext **ret) { |
89 | _cleanup_(link_config_ctx_freep) LinkConfigContext *ctx = NULL; | |
9a4b012e TG |
90 | |
91 | if (!ret) | |
92 | return -EINVAL; | |
93 | ||
afca7ac1 | 94 | ctx = new(LinkConfigContext, 1); |
9a4b012e TG |
95 | if (!ctx) |
96 | return -ENOMEM; | |
97 | ||
afca7ac1 YW |
98 | *ctx = (LinkConfigContext) { |
99 | .ethtool_fd = -1, | |
100 | .enable_name_policy = true, | |
101 | }; | |
9a4b012e | 102 | |
1cc6c93a | 103 | *ret = TAKE_PTR(ctx); |
9a4b012e TG |
104 | |
105 | return 0; | |
106 | } | |
107 | ||
418c02c3 | 108 | static int link_parse_wol_password(LinkConfig *config, const char *str) { |
d3867133 YW |
109 | _cleanup_(erase_and_freep) uint8_t *p = NULL; |
110 | int r; | |
111 | ||
418c02c3 | 112 | assert(config); |
d3867133 YW |
113 | assert(str); |
114 | ||
115 | assert_cc(sizeof(struct ether_addr) == SOPASS_MAX); | |
116 | ||
117 | p = new(uint8_t, SOPASS_MAX); | |
118 | if (!p) | |
119 | return -ENOMEM; | |
120 | ||
227e9ce2 YW |
121 | /* Reuse parse_ether_addr(), as their formats are equivalent. */ |
122 | r = parse_ether_addr(str, (struct ether_addr*) p); | |
d3867133 YW |
123 | if (r < 0) |
124 | return r; | |
125 | ||
418c02c3 YW |
126 | erase_and_free(config->wol_password); |
127 | config->wol_password = TAKE_PTR(p); | |
d3867133 YW |
128 | return 0; |
129 | } | |
130 | ||
418c02c3 | 131 | static int link_read_wol_password_from_file(LinkConfig *config) { |
d3867133 YW |
132 | _cleanup_(erase_and_freep) char *password = NULL; |
133 | int r; | |
134 | ||
418c02c3 | 135 | assert(config); |
d3867133 | 136 | |
418c02c3 | 137 | if (!config->wol_password_file) |
d3867133 YW |
138 | return 0; |
139 | ||
140 | r = read_full_file_full( | |
418c02c3 | 141 | AT_FDCWD, config->wol_password_file, UINT64_MAX, SIZE_MAX, |
d3867133 YW |
142 | READ_FULL_FILE_SECURE | READ_FULL_FILE_WARN_WORLD_READABLE | READ_FULL_FILE_CONNECT_SOCKET, |
143 | NULL, &password, NULL); | |
144 | if (r < 0) | |
145 | return r; | |
146 | ||
418c02c3 | 147 | return link_parse_wol_password(config, password); |
d3867133 YW |
148 | } |
149 | ||
418c02c3 | 150 | static int link_read_wol_password_from_cred(LinkConfig *config) { |
d3867133 YW |
151 | _cleanup_free_ char *base = NULL, *cred_name = NULL; |
152 | _cleanup_(erase_and_freep) char *password = NULL; | |
153 | int r; | |
154 | ||
418c02c3 YW |
155 | assert(config); |
156 | assert(config->filename); | |
d3867133 | 157 | |
418c02c3 | 158 | if (config->wol == UINT32_MAX) |
d3867133 | 159 | return 0; /* WakeOnLan= is not specified. */ |
418c02c3 | 160 | if (!FLAGS_SET(config->wol, WAKE_MAGICSECURE)) |
d3867133 | 161 | return 0; /* secureon is not specified in WakeOnLan=. */ |
418c02c3 | 162 | if (config->wol_password) |
d3867133 | 163 | return 0; /* WakeOnLanPassword= is specified. */ |
418c02c3 | 164 | if (config->wol_password_file) |
d3867133 YW |
165 | return 0; /* a file name is specified in WakeOnLanPassword=, but failed to read it. */ |
166 | ||
418c02c3 | 167 | r = path_extract_filename(config->filename, &base); |
d3867133 YW |
168 | if (r < 0) |
169 | return r; | |
170 | ||
171 | cred_name = strjoin(base, ".wol.password"); | |
172 | if (!cred_name) | |
173 | return -ENOMEM; | |
174 | ||
175 | r = read_credential(cred_name, (void**) &password, NULL); | |
176 | if (r == -ENOENT) | |
177 | r = read_credential("wol.password", (void**) &password, NULL); | |
178 | if (r < 0) | |
179 | return r; | |
180 | ||
418c02c3 | 181 | return link_parse_wol_password(config, password); |
d3867133 YW |
182 | } |
183 | ||
418c02c3 | 184 | static int link_adjust_wol_options(LinkConfig *config) { |
d3867133 YW |
185 | int r; |
186 | ||
418c02c3 | 187 | assert(config); |
d3867133 | 188 | |
418c02c3 | 189 | r = link_read_wol_password_from_file(config); |
d3867133 YW |
190 | if (r == -ENOMEM) |
191 | return log_oom(); | |
192 | if (r < 0) | |
418c02c3 | 193 | log_warning_errno(r, "Failed to read WakeOnLan password from %s, ignoring: %m", config->wol_password_file); |
d3867133 | 194 | |
418c02c3 | 195 | r = link_read_wol_password_from_cred(config); |
d3867133 YW |
196 | if (r == -ENOMEM) |
197 | return log_oom(); | |
198 | if (r < 0) | |
199 | log_warning_errno(r, "Failed to read WakeOnLan password from credential, ignoring: %m"); | |
200 | ||
418c02c3 | 201 | if (config->wol != UINT32_MAX && config->wol_password) |
d3867133 YW |
202 | /* Enable WAKE_MAGICSECURE flag when WakeOnLanPassword=. Note that when |
203 | * WakeOnLanPassword= is set without WakeOnLan=, then ethtool_set_wol() enables | |
204 | * WAKE_MAGICSECURE flag and other flags are not changed. */ | |
418c02c3 | 205 | config->wol |= WAKE_MAGICSECURE; |
d3867133 YW |
206 | |
207 | return 0; | |
208 | } | |
209 | ||
afca7ac1 | 210 | int link_load_one(LinkConfigContext *ctx, const char *filename) { |
418c02c3 | 211 | _cleanup_(link_config_freep) LinkConfig *config = NULL; |
6cdab9f1 | 212 | _cleanup_free_ char *name = NULL; |
e406e8a2 | 213 | const char *dropin_dirname; |
79a60834 | 214 | size_t i; |
af6f0d42 TG |
215 | int r; |
216 | ||
187dc6e5 TA |
217 | assert(ctx); |
218 | assert(filename); | |
219 | ||
e8e2788d | 220 | r = null_or_empty_path(filename); |
e8e2788d | 221 | if (r < 0) |
5672bdd3 | 222 | return log_warning_errno(r, "Failed to check if \"%s\" is empty: %m", filename); |
e8e2788d | 223 | if (r > 0) { |
ed88bcfb ZJS |
224 | log_debug("Skipping empty file: %s", filename); |
225 | return 0; | |
226 | } | |
227 | ||
6cdab9f1 YW |
228 | name = strdup(filename); |
229 | if (!name) | |
5672bdd3 | 230 | return log_oom(); |
6cdab9f1 | 231 | |
418c02c3 YW |
232 | config = new(LinkConfig, 1); |
233 | if (!config) | |
5672bdd3 | 234 | return log_oom(); |
af6f0d42 | 235 | |
418c02c3 | 236 | *config = (LinkConfig) { |
6cdab9f1 | 237 | .filename = TAKE_PTR(name), |
9e2b7763 | 238 | .mac_address_policy = MAC_ADDRESS_POLICY_NONE, |
c50404ae | 239 | .wol = UINT32_MAX, /* UINT32_MAX means do not change WOL setting. */ |
6cdab9f1 YW |
240 | .duplex = _DUP_INVALID, |
241 | .port = _NET_DEV_PORT_INVALID, | |
242 | .autonegotiation = -1, | |
a34811e4 YW |
243 | .rx_flow_control = -1, |
244 | .tx_flow_control = -1, | |
245 | .autoneg_flow_control = -1, | |
ef4a91a7 | 246 | .txqueuelen = UINT32_MAX, |
ee751240 YW |
247 | .coalesce.use_adaptive_rx_coalesce = -1, |
248 | .coalesce.use_adaptive_tx_coalesce = -1, | |
18f84f8a | 249 | .mdi = ETH_TP_MDI_INVALID, |
41ce9d76 | 250 | .sr_iov_num_vfs = UINT32_MAX, |
6cdab9f1 | 251 | }; |
5fde13d7 | 252 | |
418c02c3 YW |
253 | for (i = 0; i < ELEMENTSOF(config->features); i++) |
254 | config->features[i] = -1; | |
50725d10 | 255 | |
e406e8a2 YW |
256 | dropin_dirname = strjoina(basename(filename), ".d"); |
257 | r = config_parse_many( | |
258 | STRV_MAKE_CONST(filename), | |
259 | (const char* const*) CONF_PATHS_STRV("systemd/network"), | |
260 | dropin_dirname, | |
bd29dfef YW |
261 | "Match\0" |
262 | "Link\0" | |
263 | "SR-IOV\0", | |
e406e8a2 | 264 | config_item_perf_lookup, link_config_gperf_lookup, |
418c02c3 | 265 | CONFIG_PARSE_WARN, config, NULL); |
36f822c4 | 266 | if (r < 0) |
5672bdd3 | 267 | return r; /* config_parse_many() logs internally. */ |
af6f0d42 | 268 | |
418c02c3 | 269 | if (net_match_is_empty(&config->match) && !config->conditions) { |
dade7349 ZJS |
270 | log_warning("%s: No valid settings found in the [Match] section, ignoring file. " |
271 | "To match all interfaces, add OriginalName=* in the [Match] section.", | |
84ea567e | 272 | filename); |
dade7349 ZJS |
273 | return 0; |
274 | } | |
84ea567e | 275 | |
418c02c3 | 276 | if (!condition_test_list(config->conditions, environ, NULL, NULL, NULL)) { |
176d9c0e YW |
277 | log_debug("%s: Conditions do not match the system environment, skipping.", filename); |
278 | return 0; | |
279 | } | |
280 | ||
9e2b7763 YW |
281 | if (IN_SET(config->mac_address_policy, MAC_ADDRESS_POLICY_PERSISTENT, MAC_ADDRESS_POLICY_RANDOM) && |
282 | config->hw_addr.length > 0) | |
a7a12bf4 YW |
283 | log_warning("%s: MACAddress= in [Link] section will be ignored when MACAddressPolicy= " |
284 | "is set to \"persistent\" or \"random\".", | |
285 | filename); | |
a7a12bf4 | 286 | |
418c02c3 | 287 | r = link_adjust_wol_options(config); |
d3867133 | 288 | if (r < 0) |
5672bdd3 | 289 | return r; /* link_adjust_wol_options() logs internally. */ |
d3867133 | 290 | |
41ce9d76 | 291 | r = sr_iov_drop_invalid_sections(config->sr_iov_num_vfs, config->sr_iov_by_section); |
bd29dfef | 292 | if (r < 0) |
5672bdd3 | 293 | return r; /* sr_iov_drop_invalid_sections() logs internally. */ |
bd29dfef | 294 | |
5672bdd3 | 295 | log_debug("Parsed configuration file \"%s\"", filename); |
af6f0d42 | 296 | |
418c02c3 | 297 | LIST_PREPEND(configs, ctx->configs, TAKE_PTR(config)); |
af6f0d42 | 298 | return 0; |
af6f0d42 TG |
299 | } |
300 | ||
f6194225 | 301 | static bool enable_name_policy(void) { |
1d84ad94 | 302 | bool b; |
f6194225 | 303 | |
1d84ad94 | 304 | return proc_cmdline_get_bool("net.ifnames", &b) <= 0 || b; |
f6194225 TG |
305 | } |
306 | ||
418c02c3 | 307 | static int device_unsigned_attribute(sd_device *device, const char *attr, unsigned *type) { |
0b189e8f ZJS |
308 | const char *s; |
309 | int r; | |
310 | ||
015b097c | 311 | r = sd_device_get_sysattr_value(device, attr, &s); |
0b189e8f | 312 | if (r < 0) |
015b097c | 313 | return log_device_debug_errno(device, r, "Failed to query %s: %m", attr); |
0b189e8f ZJS |
314 | |
315 | r = safe_atou(s, type); | |
316 | if (r < 0) | |
015b097c | 317 | return log_device_warning_errno(device, r, "Failed to parse %s \"%s\": %m", attr, s); |
0b189e8f | 318 | |
015b097c | 319 | log_device_debug(device, "Device has %s=%u", attr, *type); |
0b189e8f ZJS |
320 | return 0; |
321 | } | |
322 | ||
afca7ac1 | 323 | int link_config_load(LinkConfigContext *ctx) { |
b9c54c46 | 324 | _cleanup_strv_free_ char **files = NULL; |
a39f92d3 | 325 | int r; |
af6f0d42 TG |
326 | |
327 | link_configs_free(ctx); | |
328 | ||
f6194225 TG |
329 | if (!enable_name_policy()) { |
330 | ctx->enable_name_policy = false; | |
3f85ef0f | 331 | log_info("Network interface NamePolicy= disabled on kernel command line, ignoring."); |
f6194225 TG |
332 | } |
333 | ||
97f2d76d | 334 | /* update timestamp */ |
dc0d4078 | 335 | paths_check_timestamp(NETWORK_DIRS, &ctx->network_dirs_ts_usec, true); |
af6f0d42 | 336 | |
dc0d4078 | 337 | r = conf_files_list_strv(&files, ".link", NULL, 0, NETWORK_DIRS); |
f647962d MS |
338 | if (r < 0) |
339 | return log_error_errno(r, "failed to enumerate link files: %m"); | |
af6f0d42 | 340 | |
5672bdd3 YW |
341 | STRV_FOREACH_BACKWARDS(f, files) |
342 | (void) link_load_one(ctx, *f); | |
af6f0d42 TG |
343 | |
344 | return 0; | |
345 | } | |
346 | ||
afca7ac1 | 347 | bool link_config_should_reload(LinkConfigContext *ctx) { |
dc0d4078 | 348 | return paths_check_timestamp(NETWORK_DIRS, &ctx->network_dirs_ts_usec, false); |
af6f0d42 TG |
349 | } |
350 | ||
613701a4 YW |
351 | Link *link_free(Link *link) { |
352 | if (!link) | |
353 | return NULL; | |
354 | ||
355 | sd_device_unref(link->device); | |
65022cd7 | 356 | free(link->kind); |
613701a4 YW |
357 | free(link->driver); |
358 | return mfree(link); | |
359 | } | |
360 | ||
361 | int link_new(LinkConfigContext *ctx, sd_netlink **rtnl, sd_device *device, Link **ret) { | |
362 | _cleanup_(link_freep) Link *link = NULL; | |
363 | int r; | |
af6f0d42 | 364 | |
3b64e4d4 | 365 | assert(ctx); |
751dcd8d | 366 | assert(rtnl); |
3b64e4d4 TG |
367 | assert(device); |
368 | assert(ret); | |
369 | ||
613701a4 YW |
370 | link = new(Link, 1); |
371 | if (!link) | |
372 | return -ENOMEM; | |
373 | ||
374 | *link = (Link) { | |
375 | .device = sd_device_ref(device), | |
376 | }; | |
377 | ||
378 | r = sd_device_get_sysname(device, &link->ifname); | |
4bb7cc82 YW |
379 | if (r < 0) |
380 | return r; | |
381 | ||
613701a4 | 382 | r = sd_device_get_ifindex(device, &link->ifindex); |
ef62949a YW |
383 | if (r < 0) |
384 | return r; | |
385 | ||
613701a4 | 386 | r = sd_device_get_action(device, &link->action); |
ef62949a YW |
387 | if (r < 0) |
388 | return r; | |
389 | ||
613701a4 YW |
390 | r = device_unsigned_attribute(device, "name_assign_type", &link->name_assign_type); |
391 | if (r < 0) | |
392 | log_link_debug_errno(link, r, "Failed to get \"name_assign_type\" attribute, ignoring: %m"); | |
393 | ||
394 | r = device_unsigned_attribute(device, "addr_assign_type", &link->addr_assign_type); | |
395 | if (r < 0) | |
396 | log_link_debug_errno(link, r, "Failed to get \"addr_assign_type\" attribute, ignoring: %m"); | |
70f32a26 | 397 | |
65022cd7 YW |
398 | r = rtnl_get_link_info(rtnl, link->ifindex, &link->iftype, &link->flags, |
399 | &link->kind, &link->hw_addr, &link->permanent_hw_addr); | |
613701a4 YW |
400 | if (r < 0) |
401 | return r; | |
402 | ||
403 | if (link->hw_addr.length > 0 && link->permanent_hw_addr.length == 0) { | |
404 | r = ethtool_get_permanent_hw_addr(&ctx->ethtool_fd, link->ifname, &link->permanent_hw_addr); | |
1de88f30 | 405 | if (r < 0) |
613701a4 | 406 | log_link_debug_errno(link, r, "Failed to get permanent hardware address, ignoring: %m"); |
1de88f30 | 407 | } |
4bb7cc82 | 408 | |
613701a4 YW |
409 | r = ethtool_get_driver(&ctx->ethtool_fd, link->ifname, &link->driver); |
410 | if (r < 0) | |
411 | log_link_debug_errno(link, r, "Failed to get driver, ignoring: %m"); | |
412 | ||
413 | *ret = TAKE_PTR(link); | |
414 | return 0; | |
415 | } | |
416 | ||
417 | int link_get_config(LinkConfigContext *ctx, Link *link) { | |
613701a4 YW |
418 | int r; |
419 | ||
420 | assert(ctx); | |
421 | assert(link); | |
422 | ||
423 | /* Do not configure loopback interfaces by .link files. */ | |
424 | if (link->flags & IFF_LOOPBACK) | |
425 | return -ENOENT; | |
f5767302 | 426 | |
418c02c3 | 427 | LIST_FOREACH(configs, config, ctx->configs) { |
613701a4 YW |
428 | r = net_match_config( |
429 | &config->match, | |
430 | link->device, | |
431 | &link->hw_addr, | |
432 | &link->permanent_hw_addr, | |
433 | link->driver, | |
434 | link->iftype, | |
65022cd7 | 435 | link->kind, |
613701a4 YW |
436 | link->ifname, |
437 | /* alternative_names = */ NULL, | |
438 | /* wlan_iftype = */ 0, | |
439 | /* ssid = */ NULL, | |
440 | /* bssid = */ NULL); | |
1a3caa49 YW |
441 | if (r < 0) |
442 | return r; | |
443 | if (r == 0) | |
444 | continue; | |
445 | ||
613701a4 YW |
446 | if (config->match.ifname && !strv_contains(config->match.ifname, "*") && link->name_assign_type == NET_NAME_ENUM) |
447 | log_link_warning(link, "Config file %s is applied to device based on potentially unpredictable interface name.", | |
448 | config->filename); | |
1a3caa49 | 449 | else |
613701a4 | 450 | log_link_debug(link, "Config file %s is applied", config->filename); |
1a3caa49 | 451 | |
613701a4 | 452 | link->config = config; |
1a3caa49 | 453 | return 0; |
af6f0d42 TG |
454 | } |
455 | ||
456 | return -ENOENT; | |
457 | } | |
458 | ||
613701a4 YW |
459 | static int link_apply_ethtool_settings(Link *link, int *ethtool_fd) { |
460 | LinkConfig *config; | |
2e17fed5 YW |
461 | const char *name; |
462 | int r; | |
463 | ||
613701a4 YW |
464 | assert(link); |
465 | assert(link->config); | |
2e17fed5 | 466 | assert(ethtool_fd); |
2e17fed5 | 467 | |
613701a4 YW |
468 | config = link->config; |
469 | name = link->ifname; | |
2e17fed5 YW |
470 | |
471 | r = ethtool_set_glinksettings(ethtool_fd, name, | |
472 | config->autonegotiation, config->advertise, | |
18f84f8a | 473 | config->speed, config->duplex, config->port, config->mdi); |
2e17fed5 | 474 | if (r < 0) { |
a7994dd3 | 475 | if (config->autonegotiation >= 0) |
613701a4 YW |
476 | log_link_warning_errno(link, r, "Could not %s auto negotiation, ignoring: %m", |
477 | enable_disable(config->autonegotiation)); | |
2e17fed5 YW |
478 | |
479 | if (!eqzero(config->advertise)) | |
613701a4 | 480 | log_link_warning_errno(link, r, "Could not set advertise mode, ignoring: %m"); |
a7994dd3 YW |
481 | |
482 | if (config->speed > 0) | |
613701a4 YW |
483 | log_link_warning_errno(link, r, "Could not set speed to %"PRIu64"Mbps, ignoring: %m", |
484 | DIV_ROUND_UP(config->speed, 1000000)); | |
a7994dd3 YW |
485 | |
486 | if (config->duplex >= 0) | |
613701a4 YW |
487 | log_link_warning_errno(link, r, "Could not set duplex to %s, ignoring: %m", |
488 | duplex_to_string(config->duplex)); | |
a7994dd3 YW |
489 | |
490 | if (config->port >= 0) | |
613701a4 YW |
491 | log_link_warning_errno(link, r, "Could not set port to '%s', ignoring: %m", |
492 | port_to_string(config->port)); | |
18f84f8a YW |
493 | |
494 | if (config->mdi != ETH_TP_MDI_INVALID) | |
495 | log_link_warning_errno(link, r, "Could not set MDI-X to '%s', ignoring: %m", | |
496 | mdi_to_string(config->mdi)); | |
2e17fed5 YW |
497 | } |
498 | ||
d3867133 | 499 | r = ethtool_set_wol(ethtool_fd, name, config->wol, config->wol_password); |
c50404ae YW |
500 | if (r < 0) { |
501 | _cleanup_free_ char *str = NULL; | |
502 | ||
503 | (void) wol_options_to_string_alloc(config->wol, &str); | |
613701a4 YW |
504 | log_link_warning_errno(link, r, "Could not set WakeOnLan%s%s, ignoring: %m", |
505 | isempty(str) ? "" : " to ", strempty(str)); | |
c50404ae | 506 | } |
2e17fed5 YW |
507 | |
508 | r = ethtool_set_features(ethtool_fd, name, config->features); | |
509 | if (r < 0) | |
613701a4 | 510 | log_link_warning_errno(link, r, "Could not set offload features, ignoring: %m"); |
2e17fed5 | 511 | |
80662eec YW |
512 | r = ethtool_set_channels(ethtool_fd, name, &config->channels); |
513 | if (r < 0) | |
613701a4 | 514 | log_link_warning_errno(link, r, "Could not set channels, ignoring: %m"); |
2e17fed5 | 515 | |
80662eec YW |
516 | r = ethtool_set_nic_buffer_size(ethtool_fd, name, &config->ring); |
517 | if (r < 0) | |
613701a4 | 518 | log_link_warning_errno(link, r, "Could not set ring buffer, ignoring: %m"); |
2e17fed5 | 519 | |
80662eec YW |
520 | r = ethtool_set_flow_control(ethtool_fd, name, config->rx_flow_control, config->tx_flow_control, config->autoneg_flow_control); |
521 | if (r < 0) | |
613701a4 | 522 | log_link_warning_errno(link, r, "Could not set flow control, ignoring: %m"); |
2e17fed5 | 523 | |
6c35ea5e DDM |
524 | r = ethtool_set_nic_coalesce_settings(ethtool_fd, name, &config->coalesce); |
525 | if (r < 0) | |
613701a4 | 526 | log_link_warning_errno(link, r, "Could not set coalesce settings, ignoring: %m"); |
6c35ea5e | 527 | |
2e17fed5 YW |
528 | return 0; |
529 | } | |
530 | ||
9e2b7763 YW |
531 | static bool hw_addr_is_valid(Link *link, const struct hw_addr_data *hw_addr) { |
532 | assert(link); | |
533 | assert(hw_addr); | |
534 | ||
535 | switch (link->iftype) { | |
536 | case ARPHRD_ETHER: | |
537 | /* Refuse all zero and all 0xFF. */ | |
538 | assert(hw_addr->length == ETH_ALEN); | |
539 | return !ether_addr_is_null(&hw_addr->ether) && !ether_addr_is_broadcast(&hw_addr->ether); | |
540 | ||
541 | case ARPHRD_INFINIBAND: | |
542 | /* The last 8 bytes cannot be zero*/ | |
543 | assert(hw_addr->length == INFINIBAND_ALEN); | |
544 | return !memeqzero(hw_addr->bytes + INFINIBAND_ALEN - 8, 8); | |
545 | ||
546 | default: | |
547 | assert_not_reached(); | |
548 | } | |
549 | } | |
550 | ||
551 | static int link_generate_new_hw_addr(Link *link, struct hw_addr_data *ret) { | |
552 | struct hw_addr_data hw_addr = HW_ADDR_NULL; | |
45aa0e84 | 553 | bool is_static = false; |
9e2b7763 YW |
554 | uint8_t *p; |
555 | size_t len; | |
f1ac7002 | 556 | int r; |
16b9b87a | 557 | |
613701a4 YW |
558 | assert(link); |
559 | assert(link->config); | |
613701a4 | 560 | assert(link->device); |
9e2b7763 YW |
561 | assert(ret); |
562 | ||
563 | if (link->hw_addr.length == 0) | |
564 | goto finalize; | |
565 | ||
566 | if (link->config->mac_address_policy == MAC_ADDRESS_POLICY_NONE) { | |
567 | log_link_debug(link, "Using static MAC address."); | |
568 | hw_addr = link->config->hw_addr; | |
45aa0e84 | 569 | is_static = true; |
9e2b7763 YW |
570 | goto finalize; |
571 | } | |
572 | ||
573 | if (!IN_SET(link->iftype, ARPHRD_ETHER, ARPHRD_INFINIBAND)) | |
574 | goto finalize; | |
3c9b8860 | 575 | |
613701a4 | 576 | switch (link->addr_assign_type) { |
015b097c | 577 | case NET_ADDR_SET: |
9e2b7763 YW |
578 | log_link_debug(link, "MAC address on the device already set by userspace."); |
579 | goto finalize; | |
015b097c | 580 | case NET_ADDR_STOLEN: |
9e2b7763 YW |
581 | log_link_debug(link, "MAC address on the device already set based on another device."); |
582 | goto finalize; | |
015b097c ZJS |
583 | case NET_ADDR_RANDOM: |
584 | case NET_ADDR_PERM: | |
585 | break; | |
586 | default: | |
613701a4 | 587 | log_link_warning(link, "Unknown addr_assign_type %u, ignoring", link->addr_assign_type); |
9e2b7763 | 588 | goto finalize; |
015b097c | 589 | } |
04b67d49 | 590 | |
613701a4 | 591 | if ((link->config->mac_address_policy == MAC_ADDRESS_POLICY_RANDOM) == (link->addr_assign_type == NET_ADDR_RANDOM)) { |
9e2b7763 | 592 | log_link_debug(link, "MAC address on the device already matches policy \"%s\".", |
613701a4 | 593 | mac_address_policy_to_string(link->config->mac_address_policy)); |
9e2b7763 | 594 | goto finalize; |
9d9fed9e | 595 | } |
16b9b87a | 596 | |
9e2b7763 YW |
597 | hw_addr = (struct hw_addr_data) { |
598 | .length = arphrd_to_hw_addr_len(link->iftype), | |
599 | }; | |
550c8784 | 600 | |
9e2b7763 YW |
601 | switch (link->iftype) { |
602 | case ARPHRD_ETHER: | |
603 | p = hw_addr.bytes; | |
604 | len = hw_addr.length; | |
605 | break; | |
606 | case ARPHRD_INFINIBAND: | |
607 | p = hw_addr.bytes + INFINIBAND_ALEN - 8; | |
608 | len = 8; | |
609 | break; | |
610 | default: | |
611 | assert_not_reached(); | |
612 | } | |
613 | ||
614 | if (link->config->mac_address_policy == MAC_ADDRESS_POLICY_RANDOM) | |
550c8784 | 615 | /* We require genuine randomness here, since we want to make sure we won't collide with other |
ffa047a0 | 616 | * systems booting up at the very same time. */ |
9e2b7763 | 617 | for (;;) { |
ffa047a0 | 618 | r = genuine_random_bytes(p, len, 0); |
9e2b7763 YW |
619 | if (r < 0) |
620 | return log_link_warning_errno(link, r, "Failed to acquire random data to generate MAC address: %m"); | |
621 | ||
622 | if (hw_addr_is_valid(link, &hw_addr)) | |
623 | break; | |
624 | } | |
625 | ||
626 | else { | |
dbe81cbd | 627 | uint64_t result; |
9bf3b535 | 628 | |
613701a4 | 629 | r = net_get_unique_predictable_data(link->device, |
96848152 ZJS |
630 | naming_scheme_has(NAMING_STABLE_VIRTUAL_MACS), |
631 | &result); | |
16b9b87a | 632 | if (r < 0) |
9e2b7763 | 633 | return log_link_warning_errno(link, r, "Could not generate persistent MAC address: %m"); |
16b9b87a | 634 | |
9e2b7763 YW |
635 | assert(len <= sizeof(result)); |
636 | memcpy(p, &result, len); | |
637 | if (!hw_addr_is_valid(link, &hw_addr)) | |
638 | return log_link_warning_errno(link, SYNTHETIC_ERRNO(EINVAL), | |
639 | "Could not generate valid persistent MAC address: %m"); | |
16b9b87a TG |
640 | } |
641 | ||
9e2b7763 YW |
642 | finalize: |
643 | ||
45aa0e84 | 644 | r = net_verify_hardware_address(link->ifname, is_static, link->iftype, &link->hw_addr, &hw_addr); |
9e2b7763 YW |
645 | if (r < 0) |
646 | return r; | |
647 | ||
648 | if (hw_addr_equal(&link->hw_addr, &hw_addr)) { | |
649 | *ret = HW_ADDR_NULL; | |
650 | return 0; | |
651 | } | |
652 | ||
653 | if (hw_addr.length > 0) | |
654 | log_link_debug(link, "Applying %s MAC address: %s", | |
655 | link->config->mac_address_policy == MAC_ADDRESS_POLICY_NONE ? "static" : | |
656 | mac_address_policy_to_string(link->config->mac_address_policy), | |
657 | HW_ADDR_TO_STR(&hw_addr)); | |
658 | ||
659 | *ret = hw_addr; | |
660 | return 0; | |
16b9b87a TG |
661 | } |
662 | ||
613701a4 | 663 | static int link_apply_rtnl_settings(Link *link, sd_netlink **rtnl) { |
9e2b7763 | 664 | struct hw_addr_data hw_addr = {}; |
613701a4 YW |
665 | LinkConfig *config; |
666 | int r; | |
af6f0d42 | 667 | |
613701a4 YW |
668 | assert(link); |
669 | assert(link->config); | |
2e17fed5 | 670 | assert(rtnl); |
3e137a1b | 671 | |
613701a4 | 672 | config = link->config; |
af6f0d42 | 673 | |
9e2b7763 | 674 | (void) link_generate_new_hw_addr(link, &hw_addr); |
af6f0d42 | 675 | |
9e2b7763 | 676 | r = rtnl_set_link_properties(rtnl, link->ifindex, config->alias, &hw_addr, |
face9fcc YW |
677 | config->txqueues, config->rxqueues, config->txqueuelen, |
678 | config->mtu, config->gso_max_size, config->gso_max_segments); | |
50725d10 | 679 | if (r < 0) |
613701a4 YW |
680 | log_link_warning_errno(link, r, |
681 | "Could not set Alias=, MACAddress=/MACAddressPolicy=, " | |
682 | "TransmitQueues=, ReceiveQueues=, TransmitQueueLength=, MTUBytes=, " | |
683 | "GenericSegmentOffloadMaxBytes= or GenericSegmentOffloadMaxSegments=, " | |
684 | "ignoring: %m"); | |
50725d10 | 685 | |
2e17fed5 YW |
686 | return 0; |
687 | } | |
224ded67 | 688 | |
613701a4 YW |
689 | static int link_generate_new_name(Link *link, bool enable_name_policy) { |
690 | LinkConfig *config; | |
691 | sd_device *device; | |
a34811e4 | 692 | |
613701a4 YW |
693 | assert(link); |
694 | assert(link->config); | |
695 | assert(link->device); | |
43b3a5ef | 696 | |
613701a4 YW |
697 | config = link->config; |
698 | device = link->device; | |
0b189e8f | 699 | |
613701a4 YW |
700 | if (link->action == SD_DEVICE_MOVE) { |
701 | log_link_debug(link, "Skipping to apply Name= and NamePolicy= on '%s' uevent.", | |
702 | device_action_to_string(link->action)); | |
73d2bb08 ZJS |
703 | goto no_rename; |
704 | } | |
705 | ||
613701a4 YW |
706 | if (IN_SET(link->name_assign_type, NET_NAME_USER, NET_NAME_RENAMED) && |
707 | !naming_scheme_has(NAMING_ALLOW_RERENAMES)) { | |
708 | log_link_debug(link, "Device already has a name given by userspace, not renaming."); | |
709 | goto no_rename; | |
710 | } | |
711 | ||
712 | if (enable_name_policy && config->name_policy) | |
713 | for (NamePolicy *policy = config->name_policy; *policy != _NAMEPOLICY_INVALID; policy++) { | |
5cdb3f70 | 714 | const char *new_name = NULL; |
0b189e8f | 715 | |
613701a4 | 716 | switch (*policy) { |
0b189e8f | 717 | case NAMEPOLICY_KERNEL: |
613701a4 | 718 | if (link->name_assign_type != NET_NAME_PREDICTABLE) |
0b189e8f ZJS |
719 | continue; |
720 | ||
721 | /* The kernel claims to have given a predictable name, keep it. */ | |
613701a4 YW |
722 | log_link_debug(link, "Policy *%s*: keeping predictable kernel name", |
723 | name_policy_to_string(*policy)); | |
0b189e8f | 724 | goto no_rename; |
3907446f | 725 | case NAMEPOLICY_KEEP: |
613701a4 | 726 | if (!IN_SET(link->name_assign_type, NET_NAME_USER, NET_NAME_RENAMED)) |
3907446f ZJS |
727 | continue; |
728 | ||
613701a4 YW |
729 | log_link_debug(link, "Policy *%s*: keeping existing userspace name", |
730 | name_policy_to_string(*policy)); | |
3907446f | 731 | goto no_rename; |
0b189e8f ZJS |
732 | case NAMEPOLICY_DATABASE: |
733 | (void) sd_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE", &new_name); | |
734 | break; | |
735 | case NAMEPOLICY_ONBOARD: | |
736 | (void) sd_device_get_property_value(device, "ID_NET_NAME_ONBOARD", &new_name); | |
737 | break; | |
738 | case NAMEPOLICY_SLOT: | |
739 | (void) sd_device_get_property_value(device, "ID_NET_NAME_SLOT", &new_name); | |
740 | break; | |
741 | case NAMEPOLICY_PATH: | |
742 | (void) sd_device_get_property_value(device, "ID_NET_NAME_PATH", &new_name); | |
743 | break; | |
744 | case NAMEPOLICY_MAC: | |
745 | (void) sd_device_get_property_value(device, "ID_NET_NAME_MAC", &new_name); | |
746 | break; | |
747 | default: | |
04499a70 | 748 | assert_not_reached(); |
5fde13d7 | 749 | } |
5cdb3f70 | 750 | if (ifname_valid(new_name)) { |
613701a4 YW |
751 | log_link_debug(link, "Policy *%s* yields \"%s\".", name_policy_to_string(*policy), new_name); |
752 | link->new_name = new_name; | |
5cdb3f70 YW |
753 | return 0; |
754 | } | |
daeb71a3 | 755 | } |
daeb71a3 | 756 | |
613701a4 YW |
757 | if (link->config->name) { |
758 | log_link_debug(link, "Policies didn't yield a name, using specified Name=%s.", link->config->name); | |
759 | link->new_name = link->config->name; | |
2e17fed5 YW |
760 | return 0; |
761 | } | |
16b9b87a | 762 | |
613701a4 | 763 | log_link_debug(link, "Policies didn't yield a name and Name= is not given, not renaming."); |
2e17fed5 | 764 | no_rename: |
613701a4 | 765 | link->new_name = link->ifname; |
2e17fed5 YW |
766 | return 0; |
767 | } | |
768 | ||
613701a4 | 769 | static int link_apply_alternative_names(Link *link, sd_netlink **rtnl) { |
2e17fed5 | 770 | _cleanup_strv_free_ char **altnames = NULL, **current_altnames = NULL; |
613701a4 YW |
771 | LinkConfig *config; |
772 | sd_device *device; | |
773 | int r; | |
2e17fed5 | 774 | |
613701a4 YW |
775 | assert(link); |
776 | assert(link->config); | |
777 | assert(link->device); | |
2e17fed5 | 778 | assert(rtnl); |
2e17fed5 | 779 | |
613701a4 YW |
780 | config = link->config; |
781 | device = link->device; | |
43b3a5ef | 782 | |
ef1d2c07 YW |
783 | if (config->alternative_names) { |
784 | altnames = strv_copy(config->alternative_names); | |
785 | if (!altnames) | |
786 | return log_oom(); | |
787 | } | |
788 | ||
789 | if (config->alternative_names_policy) | |
790 | for (NamePolicy *p = config->alternative_names_policy; *p != _NAMEPOLICY_INVALID; p++) { | |
61fd7d67 | 791 | const char *n = NULL; |
ef1d2c07 YW |
792 | |
793 | switch (*p) { | |
794 | case NAMEPOLICY_DATABASE: | |
795 | (void) sd_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE", &n); | |
796 | break; | |
797 | case NAMEPOLICY_ONBOARD: | |
798 | (void) sd_device_get_property_value(device, "ID_NET_NAME_ONBOARD", &n); | |
799 | break; | |
800 | case NAMEPOLICY_SLOT: | |
801 | (void) sd_device_get_property_value(device, "ID_NET_NAME_SLOT", &n); | |
802 | break; | |
803 | case NAMEPOLICY_PATH: | |
804 | (void) sd_device_get_property_value(device, "ID_NET_NAME_PATH", &n); | |
805 | break; | |
806 | case NAMEPOLICY_MAC: | |
807 | (void) sd_device_get_property_value(device, "ID_NET_NAME_MAC", &n); | |
808 | break; | |
809 | default: | |
04499a70 | 810 | assert_not_reached(); |
ef1d2c07 YW |
811 | } |
812 | if (!isempty(n)) { | |
813 | r = strv_extend(&altnames, n); | |
814 | if (r < 0) | |
815 | return log_oom(); | |
816 | } | |
817 | } | |
818 | ||
613701a4 YW |
819 | if (link->new_name) |
820 | strv_remove(altnames, link->new_name); | |
821 | strv_remove(altnames, link->ifname); | |
97fdae33 | 822 | |
613701a4 | 823 | r = rtnl_get_link_alternative_names(rtnl, link->ifindex, ¤t_altnames); |
97fdae33 | 824 | if (r < 0) |
613701a4 | 825 | log_link_debug_errno(link, r, "Failed to get alternative names, ignoring: %m"); |
97fdae33 | 826 | |
97fdae33 YW |
827 | STRV_FOREACH(p, current_altnames) |
828 | strv_remove(altnames, *p); | |
829 | ||
ef1d2c07 | 830 | strv_uniq(altnames); |
4d016e96 | 831 | strv_sort(altnames); |
613701a4 | 832 | r = rtnl_set_link_alternative_names(rtnl, link->ifindex, altnames); |
2e17fed5 | 833 | if (r < 0) |
613701a4 YW |
834 | log_link_full_errno(link, r == -EOPNOTSUPP ? LOG_DEBUG : LOG_WARNING, r, |
835 | "Could not set AlternativeName= or apply AlternativeNamesPolicy=, ignoring: %m"); | |
2e17fed5 YW |
836 | |
837 | return 0; | |
838 | } | |
a5053a15 | 839 | |
bd29dfef YW |
840 | static int sr_iov_configure(Link *link, sd_netlink **rtnl, SRIOV *sr_iov) { |
841 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
842 | int r; | |
843 | ||
844 | assert(link); | |
845 | assert(rtnl); | |
846 | assert(link->ifindex > 0); | |
847 | ||
848 | if (!*rtnl) { | |
849 | r = sd_netlink_open(rtnl); | |
850 | if (r < 0) | |
851 | return r; | |
852 | } | |
853 | ||
854 | r = sd_rtnl_message_new_link(*rtnl, &req, RTM_SETLINK, link->ifindex); | |
855 | if (r < 0) | |
856 | return r; | |
857 | ||
858 | r = sr_iov_set_netlink_message(sr_iov, req); | |
859 | if (r < 0) | |
860 | return r; | |
861 | ||
862 | r = sd_netlink_call(*rtnl, req, 0, NULL); | |
863 | if (r < 0) | |
864 | return r; | |
865 | ||
866 | return 0; | |
867 | } | |
868 | ||
869 | static int link_apply_sr_iov_config(Link *link, sd_netlink **rtnl) { | |
870 | SRIOV *sr_iov; | |
252e96ea | 871 | uint32_t n; |
bd29dfef YW |
872 | int r; |
873 | ||
874 | assert(link); | |
875 | assert(link->config); | |
41ce9d76 YW |
876 | assert(link->device); |
877 | ||
878 | r = sr_iov_set_num_vfs(link->device, link->config->sr_iov_num_vfs, link->config->sr_iov_by_section); | |
879 | if (r < 0) | |
880 | log_link_warning_errno(link, r, "Failed to set the number of SR-IOV virtual functions, ignoring: %m"); | |
bd29dfef | 881 | |
252e96ea YW |
882 | if (ordered_hashmap_isempty(link->config->sr_iov_by_section)) |
883 | return 0; | |
884 | ||
885 | r = sr_iov_get_num_vfs(link->device, &n); | |
886 | if (r < 0) { | |
887 | log_link_warning_errno(link, r, "Failed to get the number of SR-IOV virtual functions, ignoring [SR-IOV] sections: %m"); | |
888 | return 0; | |
889 | } | |
890 | if (n == 0) { | |
891 | log_link_warning(link, "No SR-IOV virtual function exists, ignoring [SR-IOV] sections: %m"); | |
892 | return 0; | |
893 | } | |
894 | ||
bd29dfef | 895 | ORDERED_HASHMAP_FOREACH(sr_iov, link->config->sr_iov_by_section) { |
252e96ea YW |
896 | if (sr_iov->vf >= n) { |
897 | log_link_warning(link, "SR-IOV virtual function %"PRIu32" does not exist, ignoring.", sr_iov->vf); | |
898 | continue; | |
899 | } | |
900 | ||
bd29dfef YW |
901 | r = sr_iov_configure(link, rtnl, sr_iov); |
902 | if (r < 0) | |
903 | log_link_warning_errno(link, r, | |
904 | "Failed to configure SR-IOV virtual function %"PRIu32", ignoring: %m", | |
905 | sr_iov->vf); | |
906 | } | |
907 | ||
908 | return 0; | |
909 | } | |
910 | ||
613701a4 | 911 | int link_apply_config(LinkConfigContext *ctx, sd_netlink **rtnl, Link *link) { |
2e17fed5 YW |
912 | int r; |
913 | ||
914 | assert(ctx); | |
751dcd8d | 915 | assert(rtnl); |
613701a4 | 916 | assert(link); |
2e17fed5 | 917 | |
613701a4 YW |
918 | if (!IN_SET(link->action, SD_DEVICE_ADD, SD_DEVICE_BIND, SD_DEVICE_MOVE)) { |
919 | log_link_debug(link, "Skipping to apply .link settings on '%s' uevent.", | |
920 | device_action_to_string(link->action)); | |
e0e789c1 | 921 | |
613701a4 | 922 | link->new_name = link->ifname; |
e0e789c1 YW |
923 | return 0; |
924 | } | |
925 | ||
613701a4 | 926 | r = link_apply_ethtool_settings(link, &ctx->ethtool_fd); |
2e17fed5 YW |
927 | if (r < 0) |
928 | return r; | |
929 | ||
613701a4 | 930 | r = link_apply_rtnl_settings(link, rtnl); |
2e17fed5 YW |
931 | if (r < 0) |
932 | return r; | |
d95b83b8 | 933 | |
613701a4 | 934 | r = link_generate_new_name(link, ctx->enable_name_policy); |
e5eadf53 YW |
935 | if (r < 0) |
936 | return r; | |
847a8a5f | 937 | |
613701a4 | 938 | r = link_apply_alternative_names(link, rtnl); |
847a8a5f TG |
939 | if (r < 0) |
940 | return r; | |
941 | ||
bd29dfef YW |
942 | r = link_apply_sr_iov_config(link, rtnl); |
943 | if (r < 0) | |
944 | return r; | |
945 | ||
847a8a5f TG |
946 | return 0; |
947 | } | |
948 | ||
fc27088a YW |
949 | int config_parse_ifalias( |
950 | const char *unit, | |
951 | const char *filename, | |
952 | unsigned line, | |
953 | const char *section, | |
954 | unsigned section_line, | |
955 | const char *lvalue, | |
956 | int ltype, | |
957 | const char *rvalue, | |
958 | void *data, | |
959 | void *userdata) { | |
960 | ||
961 | char **s = data; | |
962 | ||
963 | assert(filename); | |
964 | assert(lvalue); | |
965 | assert(rvalue); | |
966 | assert(data); | |
967 | ||
968 | if (!isempty(rvalue)) { | |
969 | *s = mfree(*s); | |
970 | return 0; | |
971 | } | |
972 | ||
973 | if (!ascii_is_valid(rvalue)) { | |
974 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
975 | "Interface alias is not ASCII clean, ignoring assignment: %s", rvalue); | |
976 | return 0; | |
977 | } | |
978 | ||
979 | if (strlen(rvalue) >= IFALIASZ) { | |
980 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
981 | "Interface alias is too long, ignoring assignment: %s", rvalue); | |
982 | return 0; | |
983 | } | |
984 | ||
b3f9c17a | 985 | return free_and_strdup_warn(s, rvalue); |
fc27088a YW |
986 | } |
987 | ||
face9fcc YW |
988 | int config_parse_rx_tx_queues( |
989 | const char *unit, | |
990 | const char *filename, | |
991 | unsigned line, | |
992 | const char *section, | |
993 | unsigned section_line, | |
994 | const char *lvalue, | |
995 | int ltype, | |
996 | const char *rvalue, | |
997 | void *data, | |
998 | void *userdata) { | |
999 | ||
1000 | uint32_t k, *v = data; | |
1001 | int r; | |
1002 | ||
1003 | if (isempty(rvalue)) { | |
1004 | *v = 0; | |
1005 | return 0; | |
1006 | } | |
1007 | ||
1008 | r = safe_atou32(rvalue, &k); | |
1009 | if (r < 0) { | |
1010 | log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s=, ignoring assignment: %s.", lvalue, rvalue); | |
1011 | return 0; | |
1012 | } | |
1013 | if (k == 0 || k > 4096) { | |
1014 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid %s=, ignoring assignment: %s.", lvalue, rvalue); | |
1015 | return 0; | |
1016 | } | |
1017 | ||
1018 | *v = k; | |
1019 | return 0; | |
1020 | } | |
1021 | ||
ef4a91a7 UÖ |
1022 | int config_parse_txqueuelen( |
1023 | const char *unit, | |
1024 | const char *filename, | |
1025 | unsigned line, | |
1026 | const char *section, | |
1027 | unsigned section_line, | |
1028 | const char *lvalue, | |
1029 | int ltype, | |
1030 | const char *rvalue, | |
1031 | void *data, | |
1032 | void *userdata) { | |
1033 | ||
1034 | uint32_t k, *v = data; | |
1035 | int r; | |
1036 | ||
1037 | if (isempty(rvalue)) { | |
1038 | *v = UINT32_MAX; | |
1039 | return 0; | |
1040 | } | |
1041 | ||
1042 | r = safe_atou32(rvalue, &k); | |
1043 | if (r < 0) { | |
1044 | log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s=, ignoring assignment: %s.", lvalue, rvalue); | |
1045 | return 0; | |
1046 | } | |
1047 | if (k == UINT32_MAX) { | |
1048 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid %s=, ignoring assignment: %s.", lvalue, rvalue); | |
1049 | return 0; | |
1050 | } | |
1051 | ||
1052 | *v = k; | |
1053 | return 0; | |
1054 | } | |
1055 | ||
d3867133 YW |
1056 | int config_parse_wol_password( |
1057 | const char *unit, | |
1058 | const char *filename, | |
1059 | unsigned line, | |
1060 | const char *section, | |
1061 | unsigned section_line, | |
1062 | const char *lvalue, | |
1063 | int ltype, | |
1064 | const char *rvalue, | |
1065 | void *data, | |
1066 | void *userdata) { | |
1067 | ||
418c02c3 | 1068 | LinkConfig *config = userdata; |
d3867133 YW |
1069 | int r; |
1070 | ||
1071 | assert(filename); | |
1072 | assert(lvalue); | |
1073 | assert(rvalue); | |
1074 | assert(userdata); | |
1075 | ||
1076 | if (isempty(rvalue)) { | |
418c02c3 YW |
1077 | config->wol_password = erase_and_free(config->wol_password); |
1078 | config->wol_password_file = mfree(config->wol_password_file); | |
d3867133 YW |
1079 | return 0; |
1080 | } | |
1081 | ||
1082 | if (path_is_absolute(rvalue) && path_is_safe(rvalue)) { | |
418c02c3 YW |
1083 | config->wol_password = erase_and_free(config->wol_password); |
1084 | return free_and_strdup_warn(&config->wol_password_file, rvalue); | |
d3867133 YW |
1085 | } |
1086 | ||
1087 | warn_file_is_world_accessible(filename, NULL, unit, line); | |
1088 | ||
418c02c3 | 1089 | r = link_parse_wol_password(config, rvalue); |
d3867133 YW |
1090 | if (r == -ENOMEM) |
1091 | return log_oom(); | |
1092 | if (r < 0) { | |
1093 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1094 | "Failed to parse %s=, ignoring assignment: %s.", lvalue, rvalue); | |
1095 | return 0; | |
1096 | } | |
1097 | ||
418c02c3 | 1098 | config->wol_password_file = mfree(config->wol_password_file); |
d3867133 YW |
1099 | return 0; |
1100 | } | |
1101 | ||
54ed9f88 ZJS |
1102 | static const char* const mac_address_policy_table[_MAC_ADDRESS_POLICY_MAX] = { |
1103 | [MAC_ADDRESS_POLICY_PERSISTENT] = "persistent", | |
1104 | [MAC_ADDRESS_POLICY_RANDOM] = "random", | |
1105 | [MAC_ADDRESS_POLICY_NONE] = "none", | |
be32eb9b TG |
1106 | }; |
1107 | ||
54ed9f88 | 1108 | DEFINE_STRING_TABLE_LOOKUP(mac_address_policy, MACAddressPolicy); |
d03cb6b8 YW |
1109 | DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT( |
1110 | config_parse_mac_address_policy, | |
1111 | mac_address_policy, | |
1112 | MACAddressPolicy, | |
1113 | MAC_ADDRESS_POLICY_NONE, | |
1114 | "Failed to parse MAC address policy"); | |
be32eb9b | 1115 | |
464cf22f TG |
1116 | DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy, |
1117 | _NAMEPOLICY_INVALID, | |
1118 | "Failed to parse interface name policy"); | |
ef1d2c07 | 1119 | |
ef1d2c07 YW |
1120 | DEFINE_CONFIG_PARSE_ENUMV(config_parse_alternative_names_policy, alternative_names_policy, NamePolicy, |
1121 | _NAMEPOLICY_INVALID, | |
1122 | "Failed to parse alternative names policy"); |