DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
-bool ifname_valid_full(const char *p, bool alternative) {
+bool ifname_valid_full(const char *p, IfnameValidFlags flags) {
bool numeric = true;
/* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources
* but slightly stricter, as we only allow non-control, non-space ASCII characters in the interface name. We
* also don't permit names that only container numbers, to avoid confusion with numeric interface indexes. */
+ assert(!(flags & ~_IFNAME_VALID_ALL));
+
if (isempty(p))
return false;
- if (alternative) {
+ if (flags & IFNAME_VALID_ALTERNATIVE) {
if (strlen(p) >= ALTIFNAMSIZ)
return false;
} else {
if (dot_or_dot_dot(p))
return false;
- while (*p) {
- if ((unsigned char) *p >= 127U)
+ for (const char *t = p; *t; t++) {
+ if ((unsigned char) *t >= 127U)
return false;
- if ((unsigned char) *p <= 32U)
+ if ((unsigned char) *t <= 32U)
return false;
- if (IN_SET(*p, ':', '/'))
+ if (IN_SET(*t, ':', '/'))
return false;
- numeric = numeric && (*p >= '0' && *p <= '9');
- p++;
+ numeric = numeric && (*t >= '0' && *t <= '9');
}
- if (numeric)
- return false;
+ if (numeric) {
+ if (!(flags & IFNAME_VALID_NUMERIC))
+ return false;
+
+ /* Verify that the number is well-formatted and in range. */
+ if (parse_ifindex(p) < 0)
+ return false;
+ }
return true;
}
int ip_tos_to_string_alloc(int i, char **s);
int ip_tos_from_string(const char *s);
-bool ifname_valid_full(const char *p, bool alternative);
+typedef enum {
+ IFNAME_VALID_ALTERNATIVE = 1 << 0,
+ IFNAME_VALID_NUMERIC = 1 << 1,
+ _IFNAME_VALID_ALL = IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC,
+} IfnameValidFlags;
+bool ifname_valid_full(const char *p, IfnameValidFlags flags);
static inline bool ifname_valid(const char *p) {
- return ifname_valid_full(p, false);
+ return ifname_valid_full(p, 0);
}
bool address_label_valid(const char *p);
Match.WLANInterfaceType, config_parse_match_strv, 0, offsetof(Network, match_wlan_iftype)
Match.SSID, config_parse_match_strv, 0, offsetof(Network, match_ssid)
Match.BSSID, config_parse_hwaddrs, 0, offsetof(Network, match_bssid)
-Match.Name, config_parse_match_ifnames, 1, offsetof(Network, match_name)
+Match.Name, config_parse_match_ifnames, IFNAME_VALID_ALTERNATIVE, offsetof(Network, match_name)
Match.Property, config_parse_match_property, 0, offsetof(Network, match_property)
Match.Host, config_parse_net_condition, CONDITION_HOST, offsetof(Network, conditions)
Match.Virtualization, config_parse_net_condition, CONDITION_VIRTUALIZATION, offsetof(Network, conditions)
static void test_ifname_valid(void) {
log_info("/* %s */", __func__);
- assert(ifname_valid("foo"));
- assert(ifname_valid("eth0"));
+ assert( ifname_valid("foo"));
+ assert( ifname_valid("eth0"));
assert(!ifname_valid("0"));
assert(!ifname_valid("99"));
- assert(ifname_valid("a99"));
- assert(ifname_valid("99a"));
+ assert( ifname_valid("a99"));
+ assert( ifname_valid("99a"));
assert(!ifname_valid(NULL));
assert(!ifname_valid(""));
assert(ifname_valid("foo.bar"));
assert(!ifname_valid("x:y"));
- assert(ifname_valid("xxxxxxxxxxxxxxx"));
- assert(!ifname_valid("xxxxxxxxxxxxxxxx"));
- assert(ifname_valid_full("xxxxxxxxxxxxxxxx", true));
+ assert( ifname_valid_full("xxxxxxxxxxxxxxx", 0));
+ assert(!ifname_valid_full("xxxxxxxxxxxxxxxx", 0));
+ assert( ifname_valid_full("xxxxxxxxxxxxxxxx", IFNAME_VALID_ALTERNATIVE));
+ assert( ifname_valid_full("xxxxxxxxxxxxxxxx", IFNAME_VALID_ALTERNATIVE));
+ assert(!ifname_valid_full("999", IFNAME_VALID_ALTERNATIVE));
+ assert( ifname_valid_full("999", IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC));
+ assert(!ifname_valid_full("0", IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC));
}
static void test_socket_print_unix_one(const char *in, size_t len_in, const char *expected) {
#include "ethtool-util.h"
#include "link-config.h"
#include "network-internal.h"
+#include "socket-util.h"
%}
struct ConfigPerfItem;
%null_strings
Link.MACAddress, config_parse_hwaddr, 0, offsetof(link_config, mac)
Link.NamePolicy, config_parse_name_policy, 0, offsetof(link_config, name_policy)
Link.Name, config_parse_ifname, 0, offsetof(link_config, name)
-Link.AlternativeName, config_parse_ifnames, 1, offsetof(link_config, alternative_names)
+Link.AlternativeName, config_parse_ifnames, IFNAME_VALID_ALTERNATIVE, offsetof(link_config, alternative_names)
Link.AlternativeNamesPolicy, config_parse_alternative_names_policy, 0, offsetof(link_config, alternative_names_policy)
Link.Alias, config_parse_ifalias, 0, offsetof(link_config, alias)
Link.MTUBytes, config_parse_mtu, AF_UNSPEC, offsetof(link_config, mtu)