basic: convert ifname_valid_full() to take flags and allow numeric interfaces
This commit is contained in:
parent
25b2d602b9
commit
2313524aa0
|
@ -686,17 +686,19 @@ static const char* const ip_tos_table[] = {
|
||||||
|
|
||||||
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
|
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;
|
bool numeric = true;
|
||||||
|
|
||||||
/* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources
|
/* 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
|
* 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. */
|
* also don't permit names that only container numbers, to avoid confusion with numeric interface indexes. */
|
||||||
|
|
||||||
|
assert(!(flags & ~_IFNAME_VALID_ALL));
|
||||||
|
|
||||||
if (isempty(p))
|
if (isempty(p))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (alternative) {
|
if (flags & IFNAME_VALID_ALTERNATIVE) {
|
||||||
if (strlen(p) >= ALTIFNAMSIZ)
|
if (strlen(p) >= ALTIFNAMSIZ)
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
|
@ -707,22 +709,27 @@ bool ifname_valid_full(const char *p, bool alternative) {
|
||||||
if (dot_or_dot_dot(p))
|
if (dot_or_dot_dot(p))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
while (*p) {
|
for (const char *t = p; *t; t++) {
|
||||||
if ((unsigned char) *p >= 127U)
|
if ((unsigned char) *t >= 127U)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if ((unsigned char) *p <= 32U)
|
if ((unsigned char) *t <= 32U)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (IN_SET(*p, ':', '/'))
|
if (IN_SET(*t, ':', '/'))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
numeric = numeric && (*p >= '0' && *p <= '9');
|
numeric = numeric && (*t >= '0' && *t <= '9');
|
||||||
p++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (numeric)
|
if (numeric) {
|
||||||
return false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,9 +130,14 @@ static inline int fd_inc_rcvbuf(int fd, size_t n) {
|
||||||
int ip_tos_to_string_alloc(int i, char **s);
|
int ip_tos_to_string_alloc(int i, char **s);
|
||||||
int ip_tos_from_string(const 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) {
|
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);
|
bool address_label_valid(const char *p);
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ Match.Type, config_parse_match_strv,
|
||||||
Match.WLANInterfaceType, config_parse_match_strv, 0, offsetof(Network, match_wlan_iftype)
|
Match.WLANInterfaceType, config_parse_match_strv, 0, offsetof(Network, match_wlan_iftype)
|
||||||
Match.SSID, config_parse_match_strv, 0, offsetof(Network, match_ssid)
|
Match.SSID, config_parse_match_strv, 0, offsetof(Network, match_ssid)
|
||||||
Match.BSSID, config_parse_hwaddrs, 0, offsetof(Network, match_bssid)
|
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.Property, config_parse_match_property, 0, offsetof(Network, match_property)
|
||||||
Match.Host, config_parse_net_condition, CONDITION_HOST, offsetof(Network, conditions)
|
Match.Host, config_parse_net_condition, CONDITION_HOST, offsetof(Network, conditions)
|
||||||
Match.Virtualization, config_parse_net_condition, CONDITION_VIRTUALIZATION, offsetof(Network, conditions)
|
Match.Virtualization, config_parse_net_condition, CONDITION_VIRTUALIZATION, offsetof(Network, conditions)
|
||||||
|
|
|
@ -26,13 +26,13 @@ assert_cc(SUN_PATH_LEN == 108);
|
||||||
static void test_ifname_valid(void) {
|
static void test_ifname_valid(void) {
|
||||||
log_info("/* %s */", __func__);
|
log_info("/* %s */", __func__);
|
||||||
|
|
||||||
assert(ifname_valid("foo"));
|
assert( ifname_valid("foo"));
|
||||||
assert(ifname_valid("eth0"));
|
assert( ifname_valid("eth0"));
|
||||||
|
|
||||||
assert(!ifname_valid("0"));
|
assert(!ifname_valid("0"));
|
||||||
assert(!ifname_valid("99"));
|
assert(!ifname_valid("99"));
|
||||||
assert(ifname_valid("a99"));
|
assert( ifname_valid("a99"));
|
||||||
assert(ifname_valid("99a"));
|
assert( ifname_valid("99a"));
|
||||||
|
|
||||||
assert(!ifname_valid(NULL));
|
assert(!ifname_valid(NULL));
|
||||||
assert(!ifname_valid(""));
|
assert(!ifname_valid(""));
|
||||||
|
@ -44,9 +44,13 @@ static void test_ifname_valid(void) {
|
||||||
assert(ifname_valid("foo.bar"));
|
assert(ifname_valid("foo.bar"));
|
||||||
assert(!ifname_valid("x:y"));
|
assert(!ifname_valid("x:y"));
|
||||||
|
|
||||||
assert(ifname_valid("xxxxxxxxxxxxxxx"));
|
assert( ifname_valid_full("xxxxxxxxxxxxxxx", 0));
|
||||||
assert(!ifname_valid("xxxxxxxxxxxxxxxx"));
|
assert(!ifname_valid_full("xxxxxxxxxxxxxxxx", 0));
|
||||||
assert(ifname_valid_full("xxxxxxxxxxxxxxxx", true));
|
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) {
|
static void test_socket_print_unix_one(const char *in, size_t len_in, const char *expected) {
|
||||||
|
|
|
@ -7,6 +7,7 @@ _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"")
|
||||||
#include "ethtool-util.h"
|
#include "ethtool-util.h"
|
||||||
#include "link-config.h"
|
#include "link-config.h"
|
||||||
#include "network-internal.h"
|
#include "network-internal.h"
|
||||||
|
#include "socket-util.h"
|
||||||
%}
|
%}
|
||||||
struct ConfigPerfItem;
|
struct ConfigPerfItem;
|
||||||
%null_strings
|
%null_strings
|
||||||
|
@ -36,7 +37,7 @@ Link.MACAddressPolicy, config_parse_mac_address_policy, 0,
|
||||||
Link.MACAddress, config_parse_hwaddr, 0, offsetof(link_config, mac)
|
Link.MACAddress, config_parse_hwaddr, 0, offsetof(link_config, mac)
|
||||||
Link.NamePolicy, config_parse_name_policy, 0, offsetof(link_config, name_policy)
|
Link.NamePolicy, config_parse_name_policy, 0, offsetof(link_config, name_policy)
|
||||||
Link.Name, config_parse_ifname, 0, offsetof(link_config, name)
|
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.AlternativeNamesPolicy, config_parse_alternative_names_policy, 0, offsetof(link_config, alternative_names_policy)
|
||||||
Link.Alias, config_parse_ifalias, 0, offsetof(link_config, alias)
|
Link.Alias, config_parse_ifalias, 0, offsetof(link_config, alias)
|
||||||
Link.MTUBytes, config_parse_mtu, AF_UNSPEC, offsetof(link_config, mtu)
|
Link.MTUBytes, config_parse_mtu, AF_UNSPEC, offsetof(link_config, mtu)
|
||||||
|
|
Loading…
Reference in a new issue