]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/netif-naming-scheme.c
mkosi: update arch commit reference
[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 },
0a4ecc54 27 { "v257", NAMING_V257 },
35b35190 28 /* … add more schemes here, as the logic to name devices is updated … */
681cb84a
ZJS
29
30 EXTRA_NET_NAMING_MAP
35b35190
ZJS
31};
32
77faadfd 33const NamingScheme* naming_scheme_from_name(const char *name) {
ad337e55 34 /* "latest" may either be defined explicitly by the extra map, in which case we will find it in
681cb84a
ZJS
35 * the table like any other name. After iterating through the table, we check for "latest" again,
36 * which means that if not mapped explicitly, it maps to the last defined entry, whatever that is. */
35b35190 37
acaa6368 38 for (size_t i = 0; i < ELEMENTSOF(naming_schemes); i++)
35b35190
ZJS
39 if (streq(naming_schemes[i].name, name))
40 return naming_schemes + i;
41
681cb84a
ZJS
42 if (streq(name, "latest"))
43 return naming_schemes + ELEMENTSOF(naming_schemes) - 1;
44
35b35190
ZJS
45 return NULL;
46}
47
48const NamingScheme* naming_scheme(void) {
49 static const NamingScheme *cache = NULL;
50 _cleanup_free_ char *buffer = NULL;
51 const char *e, *k;
52
53 if (cache)
54 return cache;
55
56 /* Acquire setting from the kernel command line */
78266a54 57 (void) proc_cmdline_get_key("net.naming_scheme", 0, &buffer);
35b35190
ZJS
58
59 /* Also acquire it from an env var */
60 e = getenv("NET_NAMING_SCHEME");
61 if (e) {
62 if (*e == ':') {
63 /* If prefixed with ':' the kernel cmdline takes precedence */
64 k = buffer ?: e + 1;
65 } else
66 k = e; /* Otherwise the env var takes precedence */
67 } else
68 k = buffer;
69
70 if (k) {
71 cache = naming_scheme_from_name(k);
72 if (cache) {
73 log_info("Using interface naming scheme '%s'.", cache->name);
74 return cache;
75 }
76
77 log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k);
78 }
79
80 cache = naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME);
81 assert(cache);
82 log_info("Using default interface naming scheme '%s'.", cache->name);
83
84 return cache;
85}
ff516b43
YW
86
87static const char* const name_policy_table[_NAMEPOLICY_MAX] = {
8b018319
ZJS
88 [NAMEPOLICY_KERNEL] = "kernel",
89 [NAMEPOLICY_KEEP] = "keep",
ff516b43 90 [NAMEPOLICY_DATABASE] = "database",
8b018319
ZJS
91 [NAMEPOLICY_ONBOARD] = "onboard",
92 [NAMEPOLICY_SLOT] = "slot",
93 [NAMEPOLICY_PATH] = "path",
94 [NAMEPOLICY_MAC] = "mac",
ff516b43
YW
95};
96
97DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
98
99static const char* const alternative_names_policy_table[_NAMEPOLICY_MAX] = {
100 [NAMEPOLICY_DATABASE] = "database",
8b018319
ZJS
101 [NAMEPOLICY_ONBOARD] = "onboard",
102 [NAMEPOLICY_SLOT] = "slot",
103 [NAMEPOLICY_PATH] = "path",
104 [NAMEPOLICY_MAC] = "mac",
ff516b43
YW
105};
106
107DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy, NamePolicy);
3b2e7dc5
LN
108
109static int naming_sysattr_allowed_by_default(sd_device *dev) {
110 int r;
111
112 assert(dev);
113
114 r = device_get_property_bool(dev, "ID_NET_NAME_ALLOW");
115 if (r == -ENOENT)
116 return true;
117
118 return r;
119}
120
121static int naming_sysattr_allowed(sd_device *dev, const char *sysattr) {
122 char *sysattr_property;
123 int r;
124
125 assert(dev);
126 assert(sysattr);
127
128 sysattr_property = strjoina("ID_NET_NAME_ALLOW_", sysattr);
129 ascii_strupper(sysattr_property);
130
131 r = device_get_property_bool(dev, sysattr_property);
132 if (r == -ENOENT)
133 /* If ID_NET_NAME_ALLOW is not set or set to 1 default is to allow */
134 return naming_sysattr_allowed_by_default(dev);
135
136 return r;
137}
138
139int device_get_sysattr_int_filtered(sd_device *device, const char *sysattr, int *ret_value) {
140 int r;
141
142 r = naming_sysattr_allowed(device, sysattr);
143 if (r < 0)
144 return r;
145 if (r == 0)
146 return -ENOENT;
147
148 return device_get_sysattr_int(device, sysattr, ret_value);
149}
150
151int device_get_sysattr_unsigned_filtered(sd_device *device, const char *sysattr, unsigned *ret_value) {
152 int r;
153
154 r = naming_sysattr_allowed(device, sysattr);
155 if (r < 0)
156 return r;
157 if (r == 0)
158 return -ENOENT;
159
160 return device_get_sysattr_unsigned(device, sysattr, ret_value);
161}
162
163int device_get_sysattr_bool_filtered(sd_device *device, const char *sysattr) {
164 int r;
165
166 r = naming_sysattr_allowed(device, sysattr);
167 if (r < 0)
168 return r;
169 if (r == 0)
170 return -ENOENT;
171
172 return device_get_sysattr_bool(device, sysattr);
173}
174
175int device_get_sysattr_value_filtered(sd_device *device, const char *sysattr, const char **ret_value) {
176 int r;
177
178 r = naming_sysattr_allowed(device, sysattr);
179 if (r < 0)
180 return r;
181 if (r == 0)
182 return -ENOENT;
183
184 return sd_device_get_sysattr_value(device, sysattr, ret_value);
185}