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