| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
#include "sd-device.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "device-private.h" |
| 9 |
#include "log.h" |
| 10 |
#include "netif-naming-scheme.h" |
| 11 |
#include "proc-cmdline.h" |
| 12 |
#include "string-table.h" |
| 13 |
#include "string-util.h" |
| 14 |
|
| 15 |
static const NamingScheme naming_schemes[] = { |
| 16 |
{ "v238", NAMING_V238 }, |
| 17 |
{ "v239", NAMING_V239 }, |
| 18 |
{ "v240", NAMING_V240 }, |
| 19 |
{ "v241", NAMING_V241 }, |
| 20 |
{ "v243", NAMING_V243 }, |
| 21 |
{ "v245", NAMING_V245 }, |
| 22 |
{ "v247", NAMING_V247 }, |
| 23 |
{ "v249", NAMING_V249 }, |
| 24 |
{ "v250", NAMING_V250 }, |
| 25 |
{ "v251", NAMING_V251 }, |
| 26 |
{ "v252", NAMING_V252 }, |
| 27 |
{ "v253", NAMING_V253 }, |
| 28 |
{ "v254", NAMING_V254 }, |
| 29 |
{ "v255", NAMING_V255 }, |
| 30 |
{ "v257", NAMING_V257 }, |
| 31 |
{ "v258", NAMING_V258 }, |
| 32 |
{ "v259", NAMING_V259 }, |
| 33 |
{ "v260", NAMING_V260 }, |
| 34 |
{ "v261", NAMING_V261 }, |
| 35 |
/* … add more schemes here, as the logic to name devices is updated … */ |
| 36 |
|
| 37 |
EXTRA_NET_NAMING_MAP |
| 38 |
}; |
| 39 |
|
| 40 |
const NamingScheme* naming_scheme_from_name(const char *name) { |
| 41 |
/* "latest" may either be defined explicitly by the extra map, in which case we will find it in |
| 42 |
* the table like any other name. After iterating through the table, we check for "latest" again, |
| 43 |
* which means that if not mapped explicitly, it maps to the last defined entry, whatever that is. */ |
| 44 |
|
| 45 |
FOREACH_ELEMENT(scheme, naming_schemes) |
| 46 |
if (streq(scheme->name, name)) |
| 47 |
return scheme; |
| 48 |
|
| 49 |
if (streq(name, "latest")) |
| 50 |
return naming_schemes + ELEMENTSOF(naming_schemes) - 1; |
| 51 |
|
| 52 |
return NULL; |
| 53 |
} |
| 54 |
|
| 55 |
const NamingScheme* naming_scheme(void) { |
| 56 |
static const NamingScheme *cache = NULL; |
| 57 |
_cleanup_free_ char *buffer = NULL; |
| 58 |
const char *e, *k; |
| 59 |
|
| 60 |
if (cache) |
| 61 |
return cache; |
| 62 |
|
| 63 |
/* Acquire setting from the kernel command line */ |
| 64 |
(void) proc_cmdline_get_key("net.naming_scheme", 0, &buffer); |
| 65 |
|
| 66 |
/* Also acquire it from an env var */ |
| 67 |
e = getenv("NET_NAMING_SCHEME"); |
| 68 |
if (e) { |
| 69 |
if (*e == ':') { |
| 70 |
/* If prefixed with ':' the kernel cmdline takes precedence */ |
| 71 |
k = buffer ?: e + 1; |
| 72 |
} else |
| 73 |
k = e; /* Otherwise the env var takes precedence */ |
| 74 |
} else |
| 75 |
k = buffer; |
| 76 |
|
| 77 |
if (k) { |
| 78 |
cache = naming_scheme_from_name(k); |
| 79 |
if (cache) { |
| 80 |
log_info("Using interface naming scheme '%s'.", cache->name); |
| 81 |
return cache; |
| 82 |
} |
| 83 |
|
| 84 |
log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k); |
| 85 |
} |
| 86 |
|
| 87 |
cache = naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME); |
| 88 |
assert(cache); |
| 89 |
log_info("Using default interface naming scheme '%s'.", cache->name); |
| 90 |
|
| 91 |
return cache; |
| 92 |
} |
| 93 |
|
| 94 |
static const char* const name_policy_table[_NAMEPOLICY_MAX] = { |
| 95 |
[NAMEPOLICY_KERNEL] = "kernel", |
| 96 |
[NAMEPOLICY_KEEP] = "keep", |
| 97 |
[NAMEPOLICY_DATABASE] = "database", |
| 98 |
[NAMEPOLICY_ONBOARD] = "onboard", |
| 99 |
[NAMEPOLICY_SLOT] = "slot", |
| 100 |
[NAMEPOLICY_PATH] = "path", |
| 101 |
[NAMEPOLICY_MAC] = "mac", |
| 102 |
}; |
| 103 |
|
| 104 |
DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy); |
| 105 |
|
| 106 |
static const char* const alternative_names_policy_table[_NAMEPOLICY_MAX] = { |
| 107 |
[NAMEPOLICY_DATABASE] = "database", |
| 108 |
[NAMEPOLICY_ONBOARD] = "onboard", |
| 109 |
[NAMEPOLICY_SLOT] = "slot", |
| 110 |
[NAMEPOLICY_PATH] = "path", |
| 111 |
[NAMEPOLICY_MAC] = "mac", |
| 112 |
}; |
| 113 |
|
| 114 |
DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy, NamePolicy); |
| 115 |
|
| 116 |
static int naming_sysattr_allowed_by_default(sd_device *dev) { |
| 117 |
int r; |
| 118 |
|
| 119 |
assert(dev); |
| 120 |
|
| 121 |
r = device_get_property_bool(dev, "ID_NET_NAME_ALLOW"); |
| 122 |
if (r == -ENOENT) |
| 123 |
return true; |
| 124 |
|
| 125 |
return r; |
| 126 |
} |
| 127 |
|
| 128 |
static int naming_sysattr_allowed(sd_device *dev, const char *sysattr) { |
| 129 |
char *sysattr_property; |
| 130 |
int r; |
| 131 |
|
| 132 |
assert(dev); |
| 133 |
assert(sysattr); |
| 134 |
|
| 135 |
sysattr_property = strjoina("ID_NET_NAME_ALLOW_", sysattr); |
| 136 |
ascii_strupper(sysattr_property); |
| 137 |
|
| 138 |
r = device_get_property_bool(dev, sysattr_property); |
| 139 |
if (r == -ENOENT) |
| 140 |
/* If ID_NET_NAME_ALLOW is not set or set to 1 default is to allow */ |
| 141 |
return naming_sysattr_allowed_by_default(dev); |
| 142 |
|
| 143 |
return r; |
| 144 |
} |
| 145 |
|
| 146 |
int device_get_sysattr_int_filtered(sd_device *device, const char *sysattr, int *ret_value) { |
| 147 |
int r; |
| 148 |
|
| 149 |
r = naming_sysattr_allowed(device, sysattr); |
| 150 |
if (r < 0) |
| 151 |
return r; |
| 152 |
if (r == 0) |
| 153 |
return -ENOENT; |
| 154 |
|
| 155 |
return device_get_sysattr_int(device, sysattr, ret_value); |
| 156 |
} |
| 157 |
|
| 158 |
int device_get_sysattr_unsigned_filtered(sd_device *device, const char *sysattr, unsigned *ret_value) { |
| 159 |
int r; |
| 160 |
|
| 161 |
r = naming_sysattr_allowed(device, sysattr); |
| 162 |
if (r < 0) |
| 163 |
return r; |
| 164 |
if (r == 0) |
| 165 |
return -ENOENT; |
| 166 |
|
| 167 |
return device_get_sysattr_unsigned(device, sysattr, ret_value); |
| 168 |
} |
| 169 |
|
| 170 |
int device_get_sysattr_bool_filtered(sd_device *device, const char *sysattr) { |
| 171 |
int r; |
| 172 |
|
| 173 |
r = naming_sysattr_allowed(device, sysattr); |
| 174 |
if (r < 0) |
| 175 |
return r; |
| 176 |
if (r == 0) |
| 177 |
return -ENOENT; |
| 178 |
|
| 179 |
return device_get_sysattr_bool(device, sysattr); |
| 180 |
} |
| 181 |
|
| 182 |
int device_get_sysattr_safe_string_filtered(sd_device *device, const char *sysattr, const char **ret_value) { |
| 183 |
int r; |
| 184 |
|
| 185 |
r = naming_sysattr_allowed(device, sysattr); |
| 186 |
if (r < 0) |
| 187 |
return r; |
| 188 |
if (r == 0) |
| 189 |
return -ENOENT; |
| 190 |
|
| 191 |
return device_get_sysattr_safe_string(device, sysattr, ret_value); |
| 192 |
} |