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