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