]>
git.proxmox.com Git - systemd.git/blob - src/udev/net/link-config.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
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.
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.
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/>.
22 #include <netinet/ether.h>
23 #include <linux/netdevice.h>
25 #include "sd-netlink.h"
27 #include "alloc-util.h"
28 #include "conf-files.h"
29 #include "conf-parser.h"
30 #include "ethtool-util.h"
32 #include "libudev-private.h"
33 #include "link-config.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"
48 struct link_config_ctx
{
49 LIST_HEAD(link_config
, links
);
53 bool enable_name_policy
;
57 usec_t link_dirs_ts_usec
;
60 static const char* const link_dirs
[] = {
61 "/etc/systemd/network",
62 "/run/systemd/network",
63 "/usr/lib/systemd/network",
65 "/lib/systemd/network",
69 static void link_config_free(link_config
*link
) {
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
);
85 free(link
->description
);
87 free(link
->name_policy
);
94 DEFINE_TRIVIAL_CLEANUP_FUNC(link_config
*, link_config_free
);
96 static void link_configs_free(link_config_ctx
*ctx
) {
97 link_config
*link
, *link_next
;
102 LIST_FOREACH_SAFE(links
, link
, link_next
, ctx
->links
)
103 link_config_free(link
);
106 void link_config_ctx_free(link_config_ctx
*ctx
) {
110 safe_close(ctx
->ethtool_fd
);
112 sd_netlink_unref(ctx
->rtnl
);
114 link_configs_free(ctx
);
121 DEFINE_TRIVIAL_CLEANUP_FUNC(link_config_ctx
*, link_config_ctx_free
);
123 int link_config_ctx_new(link_config_ctx
**ret
) {
124 _cleanup_(link_config_ctx_freep
) link_config_ctx
*ctx
= NULL
;
129 ctx
= new0(link_config_ctx
, 1);
133 LIST_HEAD_INIT(ctx
->links
);
135 ctx
->ethtool_fd
= -1;
137 ctx
->enable_name_policy
= true;
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
;
153 file
= fopen(filename
, "re");
161 if (null_or_empty_fd(fileno(file
))) {
162 log_debug("Skipping empty file: %s", filename
);
166 link
= new0(link_config
, 1);
170 link
->mac_policy
= _MACPOLICY_INVALID
;
171 link
->wol
= _WOL_INVALID
;
172 link
->duplex
= _DUP_INVALID
;
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
);
181 log_debug("Parsed configuration file %s", filename
);
183 if (link
->mtu
> UINT_MAX
|| link
->speed
> UINT_MAX
)
186 link
->filename
= strdup(filename
);
188 LIST_PREPEND(links
, ctx
->links
, link
);
194 static bool enable_name_policy(void) {
195 _cleanup_free_
char *line
= NULL
;
196 const char *word
, *state
;
200 r
= proc_cmdline(&line
);
202 log_warning_errno(r
, "Failed to read /proc/cmdline, ignoring: %m");
206 FOREACH_WORD_QUOTED(word
, l
, line
, state
)
207 if (strneq(word
, "net.ifnames=0", l
))
213 int link_config_load(link_config_ctx
*ctx
) {
215 _cleanup_strv_free_
char **files
;
218 link_configs_free(ctx
);
220 if (!enable_name_policy()) {
221 ctx
->enable_name_policy
= false;
222 log_info("Network interface NamePolicy= disabled on kernel command line, ignoring.");
225 /* update timestamp */
226 paths_check_timestamp(link_dirs
, &ctx
->link_dirs_ts_usec
, true);
228 r
= conf_files_list_strv(&files
, ".link", NULL
, link_dirs
);
230 return log_error_errno(r
, "failed to enumerate link files: %m");
232 STRV_FOREACH_BACKWARDS(f
, files
) {
233 r
= load_link(ctx
, *f
);
241 bool link_config_should_reload(link_config_ctx
*ctx
) {
242 return paths_check_timestamp(link_dirs
, &ctx
->link_dirs_ts_usec
, false);
245 int link_config_get(link_config_ctx
*ctx
, struct udev_device
*device
,
253 LIST_FOREACH(links
, link
, ctx
->links
) {
254 const char* attr_value
;
256 attr_value
= udev_device_get_sysattr_value(device
, "address");
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
;
270 attr_value
= udev_device_get_sysattr_value(device
, "name_assign_type");
272 (void) safe_atou8(attr_value
, &name_assign_type
);
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
));
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
));
288 log_debug("Config file %s applies to device %s",
289 link
->filename
, udev_device_get_sysname(device
));
302 static bool mac_is_random(struct udev_device
*device
) {
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");
312 r
= safe_atou(s
, &type
);
316 return type
== NET_ADDR_RANDOM
;
319 static bool should_rename(struct udev_device
*device
, bool respect_predictable
) {
324 /* if we can't get the assgin type, assume we should rename */
325 s
= udev_device_get_sysattr_value(device
, "name_assign_type");
329 r
= safe_atou(s
, &type
);
335 case NET_NAME_RENAMED
:
336 /* these were already named by userspace, do not touch again */
338 case NET_NAME_PREDICTABLE
:
339 /* the kernel claims to have given a predictable name */
340 if (respect_predictable
)
345 /* the name is known to be bad, or of an unknown type */
350 static int get_mac(struct udev_device
*device
, bool want_random
,
351 struct ether_addr
*mac
) {
355 random_bytes(mac
->ether_addr_octet
, ETH_ALEN
);
359 r
= net_get_unique_predictable_data(device
, &result
);
363 assert_cc(ETH_ALEN
<= sizeof(result
));
364 memcpy(mac
->ether_addr_octet
, &result
, ETH_ALEN
);
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) */
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;
388 old_name
= udev_device_get_sysname(device
);
392 r
= ethtool_set_speed(&ctx
->ethtool_fd
, old_name
, config
->speed
/ 1024, config
->duplex
);
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
));
398 r
= ethtool_set_wol(&ctx
->ethtool_fd
, old_name
, config
->wol
);
400 log_warning_errno(r
, "Could not set WakeOnLan of %s to %s: %m",
401 old_name
, wol_to_string(config
->wol
));
403 ifindex
= udev_device_get_ifindex(device
);
405 log_warning("Could not find ifindex");
409 if (ctx
->enable_name_policy
&& config
->name_policy
) {
412 for (policy
= config
->name_policy
;
413 !new_name
&& *policy
!= _NAMEPOLICY_INVALID
; policy
++) {
415 case NAMEPOLICY_KERNEL
:
416 respect_predictable
= true;
418 case NAMEPOLICY_DATABASE
:
419 new_name
= udev_device_get_property_value(device
, "ID_NET_NAME_FROM_DATABASE");
421 case NAMEPOLICY_ONBOARD
:
422 new_name
= udev_device_get_property_value(device
, "ID_NET_NAME_ONBOARD");
424 case NAMEPOLICY_SLOT
:
425 new_name
= udev_device_get_property_value(device
, "ID_NET_NAME_SLOT");
427 case NAMEPOLICY_PATH
:
428 new_name
= udev_device_get_property_value(device
, "ID_NET_NAME_PATH");
431 new_name
= udev_device_get_property_value(device
, "ID_NET_NAME_MAC");
439 if (should_rename(device
, respect_predictable
)) {
440 /* if not set by policy, fall back manually set name */
442 new_name
= config
->name
;
446 switch (config
->mac_policy
) {
447 case MACPOLICY_PERSISTENT
:
448 if (mac_is_random(device
)) {
449 r
= get_mac(device
, false, &generated_mac
);
451 log_warning_errno(r
, "Could not generate persistent MAC address for %s: %m", old_name
);
455 mac
= &generated_mac
;
458 case MACPOLICY_RANDOM
:
459 if (!mac_is_random(device
)) {
460 r
= get_mac(device
, true, &generated_mac
);
462 log_warning_errno(r
, "Could not generate random MAC address for %s: %m", old_name
);
466 mac
= &generated_mac
;
474 r
= rtnl_set_link_properties(&ctx
->rtnl
, ifindex
, config
->alias
, mac
, config
->mtu
);
476 return log_warning_errno(r
, "Could not set Alias, MACAddress or MTU on %s: %m", old_name
);
483 int link_get_driver(link_config_ctx
*ctx
, struct udev_device
*device
, char **ret
) {
488 name
= udev_device_get_sysname(device
);
492 r
= ethtool_get_driver(&ctx
->ethtool_fd
, name
, &driver
);
500 static const char* const mac_policy_table
[_MACPOLICY_MAX
] = {
501 [MACPOLICY_PERSISTENT
] = "persistent",
502 [MACPOLICY_RANDOM
] = "random",
503 [MACPOLICY_NONE
] = "none"
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");
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"
519 DEFINE_STRING_TABLE_LOOKUP(name_policy
, NamePolicy
);
520 DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy
, name_policy
, NamePolicy
,
522 "Failed to parse interface name policy");