]> git.proxmox.com Git - systemd.git/blob - src/udev/net/link-config.c
Enable seccomp support on powerpc, ppc64el, and s390x
[systemd.git] / src / udev / net / link-config.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <netinet/ether.h>
23 #include <linux/netdevice.h>
24
25 #include "sd-netlink.h"
26
27 #include "alloc-util.h"
28 #include "conf-files.h"
29 #include "conf-parser.h"
30 #include "ethtool-util.h"
31 #include "fd-util.h"
32 #include "libudev-private.h"
33 #include "link-config.h"
34 #include "log.h"
35 #include "missing.h"
36 #include "netlink-util.h"
37 #include "network-internal.h"
38 #include "parse-util.h"
39 #include "path-util.h"
40 #include "proc-cmdline.h"
41 #include "random-util.h"
42 #include "stat-util.h"
43 #include "string-table.h"
44 #include "string-util.h"
45 #include "strv.h"
46 #include "util.h"
47
48 struct link_config_ctx {
49 LIST_HEAD(link_config, links);
50
51 int ethtool_fd;
52
53 bool enable_name_policy;
54
55 sd_netlink *rtnl;
56
57 usec_t link_dirs_ts_usec;
58 };
59
60 static const char* const link_dirs[] = {
61 "/etc/systemd/network",
62 "/run/systemd/network",
63 "/usr/lib/systemd/network",
64 #ifdef HAVE_SPLIT_USR
65 "/lib/systemd/network",
66 #endif
67 NULL};
68
69 static void link_config_free(link_config *link) {
70 if (!link)
71 return;
72
73 free(link->filename);
74
75 free(link->match_mac);
76 strv_free(link->match_path);
77 strv_free(link->match_driver);
78 strv_free(link->match_type);
79 free(link->match_name);
80 free(link->match_host);
81 free(link->match_virt);
82 free(link->match_kernel);
83 free(link->match_arch);
84
85 free(link->description);
86 free(link->mac);
87 free(link->name_policy);
88 free(link->name);
89 free(link->alias);
90
91 free(link);
92 }
93
94 DEFINE_TRIVIAL_CLEANUP_FUNC(link_config*, link_config_free);
95
96 static void link_configs_free(link_config_ctx *ctx) {
97 link_config *link, *link_next;
98
99 if (!ctx)
100 return;
101
102 LIST_FOREACH_SAFE(links, link, link_next, ctx->links)
103 link_config_free(link);
104 }
105
106 void link_config_ctx_free(link_config_ctx *ctx) {
107 if (!ctx)
108 return;
109
110 safe_close(ctx->ethtool_fd);
111
112 sd_netlink_unref(ctx->rtnl);
113
114 link_configs_free(ctx);
115
116 free(ctx);
117
118 return;
119 }
120
121 DEFINE_TRIVIAL_CLEANUP_FUNC(link_config_ctx*, link_config_ctx_free);
122
123 int link_config_ctx_new(link_config_ctx **ret) {
124 _cleanup_(link_config_ctx_freep) link_config_ctx *ctx = NULL;
125
126 if (!ret)
127 return -EINVAL;
128
129 ctx = new0(link_config_ctx, 1);
130 if (!ctx)
131 return -ENOMEM;
132
133 LIST_HEAD_INIT(ctx->links);
134
135 ctx->ethtool_fd = -1;
136
137 ctx->enable_name_policy = true;
138
139 *ret = ctx;
140 ctx = NULL;
141
142 return 0;
143 }
144
145 static int load_link(link_config_ctx *ctx, const char *filename) {
146 _cleanup_(link_config_freep) link_config *link = NULL;
147 _cleanup_fclose_ FILE *file = NULL;
148 int r;
149
150 assert(ctx);
151 assert(filename);
152
153 file = fopen(filename, "re");
154 if (!file) {
155 if (errno == ENOENT)
156 return 0;
157 else
158 return -errno;
159 }
160
161 if (null_or_empty_fd(fileno(file))) {
162 log_debug("Skipping empty file: %s", filename);
163 return 0;
164 }
165
166 link = new0(link_config, 1);
167 if (!link)
168 return log_oom();
169
170 link->mac_policy = _MACPOLICY_INVALID;
171 link->wol = _WOL_INVALID;
172 link->duplex = _DUP_INVALID;
173
174 r = config_parse(NULL, filename, file,
175 "Match\0Link\0Ethernet\0",
176 config_item_perf_lookup, link_config_gperf_lookup,
177 false, false, true, link);
178 if (r < 0)
179 return r;
180 else
181 log_debug("Parsed configuration file %s", filename);
182
183 if (link->mtu > UINT_MAX || link->speed > UINT_MAX)
184 return -ERANGE;
185
186 link->filename = strdup(filename);
187
188 LIST_PREPEND(links, ctx->links, link);
189 link = NULL;
190
191 return 0;
192 }
193
194 static bool enable_name_policy(void) {
195 _cleanup_free_ char *line = NULL;
196 const char *word, *state;
197 int r;
198 size_t l;
199
200 r = proc_cmdline(&line);
201 if (r < 0) {
202 log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
203 return true;
204 }
205
206 FOREACH_WORD_QUOTED(word, l, line, state)
207 if (strneq(word, "net.ifnames=0", l))
208 return false;
209
210 return true;
211 }
212
213 int link_config_load(link_config_ctx *ctx) {
214 int r;
215 _cleanup_strv_free_ char **files;
216 char **f;
217
218 link_configs_free(ctx);
219
220 if (!enable_name_policy()) {
221 ctx->enable_name_policy = false;
222 log_info("Network interface NamePolicy= disabled on kernel command line, ignoring.");
223 }
224
225 /* update timestamp */
226 paths_check_timestamp(link_dirs, &ctx->link_dirs_ts_usec, true);
227
228 r = conf_files_list_strv(&files, ".link", NULL, link_dirs);
229 if (r < 0)
230 return log_error_errno(r, "failed to enumerate link files: %m");
231
232 STRV_FOREACH_BACKWARDS(f, files) {
233 r = load_link(ctx, *f);
234 if (r < 0)
235 return r;
236 }
237
238 return 0;
239 }
240
241 bool link_config_should_reload(link_config_ctx *ctx) {
242 return paths_check_timestamp(link_dirs, &ctx->link_dirs_ts_usec, false);
243 }
244
245 int link_config_get(link_config_ctx *ctx, struct udev_device *device,
246 link_config **ret) {
247 link_config *link;
248
249 assert(ctx);
250 assert(device);
251 assert(ret);
252
253 LIST_FOREACH(links, link, ctx->links) {
254 const char* attr_value;
255
256 attr_value = udev_device_get_sysattr_value(device, "address");
257
258 if (net_match_config(link->match_mac, link->match_path, link->match_driver,
259 link->match_type, link->match_name, link->match_host,
260 link->match_virt, link->match_kernel, link->match_arch,
261 attr_value ? ether_aton(attr_value) : NULL,
262 udev_device_get_property_value(device, "ID_PATH"),
263 udev_device_get_driver(udev_device_get_parent(device)),
264 udev_device_get_property_value(device, "ID_NET_DRIVER"),
265 udev_device_get_devtype(device),
266 udev_device_get_sysname(device))) {
267 if (link->match_name) {
268 unsigned char name_assign_type = NET_NAME_UNKNOWN;
269
270 attr_value = udev_device_get_sysattr_value(device, "name_assign_type");
271 if (attr_value)
272 (void) safe_atou8(attr_value, &name_assign_type);
273
274 if (name_assign_type == NET_NAME_ENUM) {
275 log_warning("Config file %s applies to device based on potentially unpredictable interface name '%s'",
276 link->filename, udev_device_get_sysname(device));
277 *ret = link;
278
279 return 0;
280 } else if (name_assign_type == NET_NAME_RENAMED) {
281 log_warning("Config file %s matches device based on renamed interface name '%s', ignoring",
282 link->filename, udev_device_get_sysname(device));
283
284 continue;
285 }
286 }
287
288 log_debug("Config file %s applies to device %s",
289 link->filename, udev_device_get_sysname(device));
290
291 *ret = link;
292
293 return 0;
294 }
295 }
296
297 *ret = NULL;
298
299 return -ENOENT;
300 }
301
302 static bool mac_is_random(struct udev_device *device) {
303 const char *s;
304 unsigned type;
305 int r;
306
307 /* if we can't get the assign type, assume it is not random */
308 s = udev_device_get_sysattr_value(device, "addr_assign_type");
309 if (!s)
310 return false;
311
312 r = safe_atou(s, &type);
313 if (r < 0)
314 return false;
315
316 return type == NET_ADDR_RANDOM;
317 }
318
319 static bool should_rename(struct udev_device *device, bool respect_predictable) {
320 const char *s;
321 unsigned type;
322 int r;
323
324 /* if we can't get the assgin type, assume we should rename */
325 s = udev_device_get_sysattr_value(device, "name_assign_type");
326 if (!s)
327 return true;
328
329 r = safe_atou(s, &type);
330 if (r < 0)
331 return true;
332
333 switch (type) {
334 case NET_NAME_USER:
335 case NET_NAME_RENAMED:
336 /* these were already named by userspace, do not touch again */
337 return false;
338 case NET_NAME_PREDICTABLE:
339 /* the kernel claims to have given a predictable name */
340 if (respect_predictable)
341 return false;
342 /* fall through */
343 case NET_NAME_ENUM:
344 default:
345 /* the name is known to be bad, or of an unknown type */
346 return true;
347 }
348 }
349
350 static int get_mac(struct udev_device *device, bool want_random,
351 struct ether_addr *mac) {
352 int r;
353
354 if (want_random)
355 random_bytes(mac->ether_addr_octet, ETH_ALEN);
356 else {
357 uint64_t result;
358
359 r = net_get_unique_predictable_data(device, &result);
360 if (r < 0)
361 return r;
362
363 assert_cc(ETH_ALEN <= sizeof(result));
364 memcpy(mac->ether_addr_octet, &result, ETH_ALEN);
365 }
366
367 /* see eth_random_addr in the kernel */
368 mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
369 mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
370
371 return 0;
372 }
373
374 int link_config_apply(link_config_ctx *ctx, link_config *config,
375 struct udev_device *device, const char **name) {
376 const char *old_name;
377 const char *new_name = NULL;
378 struct ether_addr generated_mac;
379 struct ether_addr *mac = NULL;
380 bool respect_predictable = false;
381 int r, ifindex;
382
383 assert(ctx);
384 assert(config);
385 assert(device);
386 assert(name);
387
388 old_name = udev_device_get_sysname(device);
389 if (!old_name)
390 return -EINVAL;
391
392 r = ethtool_set_speed(&ctx->ethtool_fd, old_name, config->speed / 1024, config->duplex);
393 if (r < 0)
394 log_warning_errno(r, "Could not set speed or duplex of %s to %zu Mbps (%s): %m",
395 old_name, config->speed / 1024,
396 duplex_to_string(config->duplex));
397
398 r = ethtool_set_wol(&ctx->ethtool_fd, old_name, config->wol);
399 if (r < 0)
400 log_warning_errno(r, "Could not set WakeOnLan of %s to %s: %m",
401 old_name, wol_to_string(config->wol));
402
403 ifindex = udev_device_get_ifindex(device);
404 if (ifindex <= 0) {
405 log_warning("Could not find ifindex");
406 return -ENODEV;
407 }
408
409 if (ctx->enable_name_policy && config->name_policy) {
410 NamePolicy *policy;
411
412 for (policy = config->name_policy;
413 !new_name && *policy != _NAMEPOLICY_INVALID; policy++) {
414 switch (*policy) {
415 case NAMEPOLICY_KERNEL:
416 respect_predictable = true;
417 break;
418 case NAMEPOLICY_DATABASE:
419 new_name = udev_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE");
420 break;
421 case NAMEPOLICY_ONBOARD:
422 new_name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
423 break;
424 case NAMEPOLICY_SLOT:
425 new_name = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
426 break;
427 case NAMEPOLICY_PATH:
428 new_name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
429 break;
430 case NAMEPOLICY_MAC:
431 new_name = udev_device_get_property_value(device, "ID_NET_NAME_MAC");
432 break;
433 default:
434 break;
435 }
436 }
437 }
438
439 if (should_rename(device, respect_predictable)) {
440 /* if not set by policy, fall back manually set name */
441 if (!new_name)
442 new_name = config->name;
443 } else
444 new_name = NULL;
445
446 switch (config->mac_policy) {
447 case MACPOLICY_PERSISTENT:
448 if (mac_is_random(device)) {
449 r = get_mac(device, false, &generated_mac);
450 if (r == -ENOENT) {
451 log_warning_errno(r, "Could not generate persistent MAC address for %s: %m", old_name);
452 break;
453 } else if (r < 0)
454 return r;
455 mac = &generated_mac;
456 }
457 break;
458 case MACPOLICY_RANDOM:
459 if (!mac_is_random(device)) {
460 r = get_mac(device, true, &generated_mac);
461 if (r == -ENOENT) {
462 log_warning_errno(r, "Could not generate random MAC address for %s: %m", old_name);
463 break;
464 } else if (r < 0)
465 return r;
466 mac = &generated_mac;
467 }
468 break;
469 case MACPOLICY_NONE:
470 default:
471 mac = config->mac;
472 }
473
474 r = rtnl_set_link_properties(&ctx->rtnl, ifindex, config->alias, mac, config->mtu);
475 if (r < 0)
476 return log_warning_errno(r, "Could not set Alias, MACAddress or MTU on %s: %m", old_name);
477
478 *name = new_name;
479
480 return 0;
481 }
482
483 int link_get_driver(link_config_ctx *ctx, struct udev_device *device, char **ret) {
484 const char *name;
485 char *driver = NULL;
486 int r;
487
488 name = udev_device_get_sysname(device);
489 if (!name)
490 return -EINVAL;
491
492 r = ethtool_get_driver(&ctx->ethtool_fd, name, &driver);
493 if (r < 0)
494 return r;
495
496 *ret = driver;
497 return 0;
498 }
499
500 static const char* const mac_policy_table[_MACPOLICY_MAX] = {
501 [MACPOLICY_PERSISTENT] = "persistent",
502 [MACPOLICY_RANDOM] = "random",
503 [MACPOLICY_NONE] = "none"
504 };
505
506 DEFINE_STRING_TABLE_LOOKUP(mac_policy, MACPolicy);
507 DEFINE_CONFIG_PARSE_ENUM(config_parse_mac_policy, mac_policy, MACPolicy,
508 "Failed to parse MAC address policy");
509
510 static const char* const name_policy_table[_NAMEPOLICY_MAX] = {
511 [NAMEPOLICY_KERNEL] = "kernel",
512 [NAMEPOLICY_DATABASE] = "database",
513 [NAMEPOLICY_ONBOARD] = "onboard",
514 [NAMEPOLICY_SLOT] = "slot",
515 [NAMEPOLICY_PATH] = "path",
516 [NAMEPOLICY_MAC] = "mac"
517 };
518
519 DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
520 DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy,
521 _NAMEPOLICY_INVALID,
522 "Failed to parse interface name policy");