]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/net/naming-scheme.c
udev: introduce NAMING_STABLE_VIRTUAL_MACS (retroactively)
[thirdparty/systemd.git] / src / udev / net / naming-scheme.c
CommitLineData
35b35190
ZJS
1/* SPDX-License-Identifier: LGPL-2.1+ */
2#include "alloc-util.h"
3#include "naming-scheme.h"
4#include "proc-cmdline.h"
5#include "string-util.h"
6
7static const NamingScheme naming_schemes[] = {
8 { "v238", NAMING_V238 },
9 { "v239", NAMING_V239 },
10 { "v240", NAMING_V240 },
96848152 11 { "v241", NAMING_V241 },
eaa9d507 12 { "v243", NAMING_V243 },
35b35190
ZJS
13 /* … add more schemes here, as the logic to name devices is updated … */
14};
15
16static const NamingScheme* naming_scheme_from_name(const char *name) {
17 size_t i;
18
19 if (streq(name, "latest"))
20 return naming_schemes + ELEMENTSOF(naming_schemes) - 1;
21
22 for (i = 0; i < ELEMENTSOF(naming_schemes); i++)
23 if (streq(naming_schemes[i].name, name))
24 return naming_schemes + i;
25
26 return NULL;
27}
28
29const NamingScheme* naming_scheme(void) {
30 static const NamingScheme *cache = NULL;
31 _cleanup_free_ char *buffer = NULL;
32 const char *e, *k;
33
34 if (cache)
35 return cache;
36
37 /* Acquire setting from the kernel command line */
38 (void) proc_cmdline_get_key("net.naming-scheme", 0, &buffer);
39
40 /* Also acquire it from an env var */
41 e = getenv("NET_NAMING_SCHEME");
42 if (e) {
43 if (*e == ':') {
44 /* If prefixed with ':' the kernel cmdline takes precedence */
45 k = buffer ?: e + 1;
46 } else
47 k = e; /* Otherwise the env var takes precedence */
48 } else
49 k = buffer;
50
51 if (k) {
52 cache = naming_scheme_from_name(k);
53 if (cache) {
54 log_info("Using interface naming scheme '%s'.", cache->name);
55 return cache;
56 }
57
58 log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k);
59 }
60
61 cache = naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME);
62 assert(cache);
63 log_info("Using default interface naming scheme '%s'.", cache->name);
64
65 return cache;
66}