1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
6 #include "device-private.h"
7 #include "netif-naming-scheme.h"
8 #include "proc-cmdline.h"
9 #include "string-util.h"
10 #include "string-table.h"
12 static const NamingScheme naming_schemes
[] = {
13 { "v238", NAMING_V238
},
14 { "v239", NAMING_V239
},
15 { "v240", NAMING_V240
},
16 { "v241", NAMING_V241
},
17 { "v243", NAMING_V243
},
18 { "v245", NAMING_V245
},
19 { "v247", NAMING_V247
},
20 { "v249", NAMING_V249
},
21 { "v250", NAMING_V250
},
22 { "v251", NAMING_V251
},
23 { "v252", NAMING_V252
},
24 { "v253", NAMING_V253
},
25 { "v254", NAMING_V254
},
26 { "v255", NAMING_V255
},
27 /* … add more schemes here, as the logic to name devices is updated … */
32 const NamingScheme
* naming_scheme_from_name(const char *name
) {
33 /* "latest" may either be defined explicitly by the extra map, in which case we will find it in
34 * the table like any other name. After iterating through the table, we check for "latest" again,
35 * which means that if not mapped explicitly, it maps to the last defined entry, whatever that is. */
37 for (size_t i
= 0; i
< ELEMENTSOF(naming_schemes
); i
++)
38 if (streq(naming_schemes
[i
].name
, name
))
39 return naming_schemes
+ i
;
41 if (streq(name
, "latest"))
42 return naming_schemes
+ ELEMENTSOF(naming_schemes
) - 1;
47 const NamingScheme
* naming_scheme(void) {
48 static const NamingScheme
*cache
= NULL
;
49 _cleanup_free_
char *buffer
= NULL
;
55 /* Acquire setting from the kernel command line */
56 (void) proc_cmdline_get_key("net.naming-scheme", 0, &buffer
);
58 /* Also acquire it from an env var */
59 e
= getenv("NET_NAMING_SCHEME");
62 /* If prefixed with ':' the kernel cmdline takes precedence */
65 k
= e
; /* Otherwise the env var takes precedence */
70 cache
= naming_scheme_from_name(k
);
72 log_info("Using interface naming scheme '%s'.", cache
->name
);
76 log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k
);
79 cache
= naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME
);
81 log_info("Using default interface naming scheme '%s'.", cache
->name
);
86 static const char* const name_policy_table
[_NAMEPOLICY_MAX
] = {
87 [NAMEPOLICY_KERNEL
] = "kernel",
88 [NAMEPOLICY_KEEP
] = "keep",
89 [NAMEPOLICY_DATABASE
] = "database",
90 [NAMEPOLICY_ONBOARD
] = "onboard",
91 [NAMEPOLICY_SLOT
] = "slot",
92 [NAMEPOLICY_PATH
] = "path",
93 [NAMEPOLICY_MAC
] = "mac",
96 DEFINE_STRING_TABLE_LOOKUP(name_policy
, NamePolicy
);
98 static const char* const alternative_names_policy_table
[_NAMEPOLICY_MAX
] = {
99 [NAMEPOLICY_DATABASE
] = "database",
100 [NAMEPOLICY_ONBOARD
] = "onboard",
101 [NAMEPOLICY_SLOT
] = "slot",
102 [NAMEPOLICY_PATH
] = "path",
103 [NAMEPOLICY_MAC
] = "mac",
106 DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy
, NamePolicy
);
108 static int naming_sysattr_allowed_by_default(sd_device
*dev
) {
113 r
= device_get_property_bool(dev
, "ID_NET_NAME_ALLOW");
120 static int naming_sysattr_allowed(sd_device
*dev
, const char *sysattr
) {
121 char *sysattr_property
;
127 sysattr_property
= strjoina("ID_NET_NAME_ALLOW_", sysattr
);
128 ascii_strupper(sysattr_property
);
130 r
= device_get_property_bool(dev
, sysattr_property
);
132 /* If ID_NET_NAME_ALLOW is not set or set to 1 default is to allow */
133 return naming_sysattr_allowed_by_default(dev
);
138 int device_get_sysattr_int_filtered(sd_device
*device
, const char *sysattr
, int *ret_value
) {
141 r
= naming_sysattr_allowed(device
, sysattr
);
147 return device_get_sysattr_int(device
, sysattr
, ret_value
);
150 int device_get_sysattr_unsigned_filtered(sd_device
*device
, const char *sysattr
, unsigned *ret_value
) {
153 r
= naming_sysattr_allowed(device
, sysattr
);
159 return device_get_sysattr_unsigned(device
, sysattr
, ret_value
);
162 int device_get_sysattr_bool_filtered(sd_device
*device
, const char *sysattr
) {
165 r
= naming_sysattr_allowed(device
, sysattr
);
171 return device_get_sysattr_bool(device
, sysattr
);
174 int device_get_sysattr_value_filtered(sd_device
*device
, const char *sysattr
, const char **ret_value
) {
177 r
= naming_sysattr_allowed(device
, sysattr
);
183 return sd_device_get_sysattr_value(device
, sysattr
, ret_value
);