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