| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "device-private.h" |
| 4 |
#include "device-util.h" |
| 5 |
#include "link-config.h" |
| 6 |
#include "log.h" |
| 7 |
#include "netif-naming-scheme.h" |
| 8 |
#include "udev-builtin.h" |
| 9 |
|
| 10 |
static LinkConfigContext *ctx = NULL; |
| 11 |
|
| 12 |
static int builtin_net_setup_link(UdevEvent *event, int argc, char **argv) { |
| 13 |
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev); |
| 14 |
_cleanup_(link_freep) Link *link = NULL; |
| 15 |
int r; |
| 16 |
|
| 17 |
if (argc > 1) |
| 18 |
return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments."); |
| 19 |
|
| 20 |
sd_device_action_t action; |
| 21 |
r = sd_device_get_action(dev, &action); |
| 22 |
if (r < 0) |
| 23 |
return log_device_error_errno(dev, r, "Failed to get action: %m"); |
| 24 |
|
| 25 |
if (!IN_SET(action, SD_DEVICE_ADD, SD_DEVICE_BIND, SD_DEVICE_MOVE)) { |
| 26 |
log_device_debug(dev, "Not applying .link settings on '%s' uevent.", |
| 27 |
device_action_to_string(action)); |
| 28 |
|
| 29 |
/* Import previously assigned .link file name. */ |
| 30 |
(void) udev_builtin_import_property(event, "ID_NET_LINK_FILE"); |
| 31 |
(void) udev_builtin_import_property(event, "ID_NET_LINK_FILE_DROPINS"); |
| 32 |
|
| 33 |
/* Set ID_NET_NAME= with the current interface name. */ |
| 34 |
const char *value; |
| 35 |
if (naming_scheme_has(NAMING_USE_INTERFACE_PROPERTY)) |
| 36 |
r = device_get_ifname(dev, &value); |
| 37 |
else |
| 38 |
r = sd_device_get_sysname(dev, &value); |
| 39 |
if (r >= 0) |
| 40 |
(void) udev_builtin_add_property(event, "ID_NET_NAME", value); |
| 41 |
|
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
r = link_new(ctx, event, &link); |
| 46 |
if (r == -ENODEV) { |
| 47 |
log_device_debug_errno(dev, r, "Link vanished while getting information, ignoring."); |
| 48 |
return 0; |
| 49 |
} |
| 50 |
if (r < 0) |
| 51 |
return log_device_warning_errno(dev, r, "Failed to get link information: %m"); |
| 52 |
|
| 53 |
r = link_get_config(ctx, link); |
| 54 |
if (r == -ENOENT) { |
| 55 |
log_device_debug_errno(dev, r, "No matching link configuration found, ignoring device."); |
| 56 |
return 0; |
| 57 |
} |
| 58 |
if (r < 0) |
| 59 |
return log_device_error_errno(dev, r, "Failed to get link config: %m"); |
| 60 |
|
| 61 |
r = link_apply_config(ctx, link); |
| 62 |
if (r == -ENODEV) |
| 63 |
log_device_debug_errno(dev, r, "Link vanished while applying configuration, ignoring."); |
| 64 |
else if (r < 0) |
| 65 |
log_device_warning_errno(dev, r, "Could not apply link configuration, ignoring: %m"); |
| 66 |
|
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
static int builtin_net_setup_link_init(void) { |
| 71 |
int r; |
| 72 |
|
| 73 |
if (ctx) |
| 74 |
return 0; |
| 75 |
|
| 76 |
r = link_config_ctx_new(&ctx); |
| 77 |
if (r < 0) |
| 78 |
return r; |
| 79 |
|
| 80 |
r = link_config_load(ctx); |
| 81 |
if (r < 0) |
| 82 |
return r; |
| 83 |
|
| 84 |
log_debug("Loaded link configuration context."); |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
static void builtin_net_setup_link_exit(void) { |
| 89 |
ctx = link_config_ctx_free(ctx); |
| 90 |
log_debug("Unloaded link configuration context."); |
| 91 |
} |
| 92 |
|
| 93 |
static bool builtin_net_setup_link_should_reload(void) { |
| 94 |
if (!ctx) |
| 95 |
return false; |
| 96 |
|
| 97 |
if (link_config_should_reload(ctx)) { |
| 98 |
log_debug("Link configuration context needs reloading."); |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
const UdevBuiltin udev_builtin_net_setup_link = { |
| 106 |
.name = "net_setup_link", |
| 107 |
.cmd = builtin_net_setup_link, |
| 108 |
.init = builtin_net_setup_link_init, |
| 109 |
.exit = builtin_net_setup_link_exit, |
| 110 |
.should_reload = builtin_net_setup_link_should_reload, |
| 111 |
.help = "Configure network link", |
| 112 |
.run_once = false, |
| 113 |
}; |