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