Merge pull request #21276 from yuwata/ether-addr-util
ether-addr-util: introduce parse_hw_addr() and related conf parsers
This commit is contained in:
+151
-62
@@ -48,6 +48,7 @@ static void hw_addr_hash_func(const struct hw_addr_data *p, struct siphash *stat
|
||||
}
|
||||
|
||||
DEFINE_HASH_OPS(hw_addr_hash_ops, struct hw_addr_data, hw_addr_hash_func, hw_addr_compare);
|
||||
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(hw_addr_hash_ops_free, struct hw_addr_data, hw_addr_hash_func, hw_addr_compare, free);
|
||||
|
||||
char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]) {
|
||||
assert(addr);
|
||||
@@ -93,75 +94,163 @@ static void ether_addr_hash_func(const struct ether_addr *p, struct siphash *sta
|
||||
}
|
||||
|
||||
DEFINE_HASH_OPS(ether_addr_hash_ops, struct ether_addr, ether_addr_hash_func, ether_addr_compare);
|
||||
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(ether_addr_hash_ops_free, struct ether_addr, ether_addr_hash_func, ether_addr_compare, free);
|
||||
|
||||
int ether_addr_from_string(const char *s, struct ether_addr *ret) {
|
||||
size_t pos = 0, n, field;
|
||||
char sep = '\0';
|
||||
const char *hex = HEXDIGITS, *hexoff;
|
||||
size_t x;
|
||||
bool touched;
|
||||
static int parse_hw_addr_one_field(const char **s, char sep, size_t len, uint8_t *buf) {
|
||||
const char *hex = HEXDIGITS, *p;
|
||||
uint16_t data = 0;
|
||||
bool cont;
|
||||
|
||||
#define parse_fields(v) \
|
||||
for (field = 0; field < ELEMENTSOF(v); field++) { \
|
||||
touched = false; \
|
||||
for (n = 0; n < (2 * sizeof(v[0])); n++) { \
|
||||
if (s[pos] == '\0') \
|
||||
break; \
|
||||
hexoff = strchr(hex, s[pos]); \
|
||||
if (!hexoff) \
|
||||
break; \
|
||||
assert(hexoff >= hex); \
|
||||
x = hexoff - hex; \
|
||||
if (x >= 16) \
|
||||
x -= 6; /* A-F */ \
|
||||
assert(x < 16); \
|
||||
touched = true; \
|
||||
v[field] <<= 4; \
|
||||
v[field] += x; \
|
||||
pos++; \
|
||||
} \
|
||||
if (!touched) \
|
||||
return -EINVAL; \
|
||||
if (field < (ELEMENTSOF(v)-1)) { \
|
||||
if (s[pos] != sep) \
|
||||
return -EINVAL; \
|
||||
else \
|
||||
pos++; \
|
||||
} \
|
||||
assert(s);
|
||||
assert(*s);
|
||||
assert(IN_SET(len, 1, 2));
|
||||
assert(buf);
|
||||
|
||||
p = *s;
|
||||
|
||||
for (size_t i = 0; i < len * 2; i++) {
|
||||
const char *hexoff;
|
||||
size_t x;
|
||||
|
||||
if (*p == '\0' || *p == sep) {
|
||||
if (i == 0)
|
||||
return -EINVAL;
|
||||
break;
|
||||
}
|
||||
|
||||
hexoff = strchr(hex, *p);
|
||||
if (!hexoff)
|
||||
return -EINVAL;
|
||||
|
||||
assert(hexoff >= hex);
|
||||
x = hexoff - hex;
|
||||
if (x >= 16)
|
||||
x -= 6; /* A-F */
|
||||
|
||||
assert(x < 16);
|
||||
data <<= 4;
|
||||
data += x;
|
||||
|
||||
p++;
|
||||
}
|
||||
|
||||
if (*p != '\0' && *p != sep)
|
||||
return -EINVAL;
|
||||
|
||||
switch (len) {
|
||||
case 1:
|
||||
buf[0] = data;
|
||||
break;
|
||||
case 2:
|
||||
buf[0] = (data & 0xff00) >> 8;
|
||||
buf[1] = data & 0xff;
|
||||
break;
|
||||
default:
|
||||
assert_not_reached();
|
||||
}
|
||||
|
||||
cont = *p == sep;
|
||||
*s = p + cont;
|
||||
return cont;
|
||||
}
|
||||
|
||||
int parse_hw_addr_full(const char *s, size_t expected_len, struct hw_addr_data *ret) {
|
||||
size_t field_size, max_len, len = 0;
|
||||
uint8_t bytes[HW_ADDR_MAX_SIZE];
|
||||
char sep;
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
assert(expected_len <= HW_ADDR_MAX_SIZE || expected_len == SIZE_MAX);
|
||||
assert(ret);
|
||||
|
||||
/* This accepts the following formats:
|
||||
*
|
||||
* Dot separated 2 bytes format: xxyy.zzaa.bbcc
|
||||
* Colon separated 1 bytes format: xx:yy:zz:aa:bb:cc
|
||||
* Hyphen separated 1 bytes format: xx-yy-zz-aa-bb-cc
|
||||
*
|
||||
* Moreover, if expected_len == 0, 4, or 16, this also accepts:
|
||||
*
|
||||
* IPv4 format: used by IPv4 tunnel, e.g. ipgre
|
||||
* IPv6 format: used by IPv6 tunnel, e.g. ip6gre
|
||||
*
|
||||
* The expected_len argument controls the length of acceptable addresses:
|
||||
*
|
||||
* 0: accepts 4 (AF_INET), 16 (AF_INET6), 6 (ETH_ALEN), or 20 (INFINIBAND_ALEN).
|
||||
* SIZE_MAX: accepts arbitrary length, but at least one separator must be included.
|
||||
* Otherwise: accepts addresses with matching length.
|
||||
*/
|
||||
|
||||
if (IN_SET(expected_len, 0, sizeof(struct in_addr), sizeof(struct in6_addr))) {
|
||||
union in_addr_union a;
|
||||
int family;
|
||||
|
||||
if (expected_len == 0)
|
||||
r = in_addr_from_string_auto(s, &family, &a);
|
||||
else {
|
||||
family = expected_len == sizeof(struct in_addr) ? AF_INET : AF_INET6;
|
||||
r = in_addr_from_string(family, s, &a);
|
||||
}
|
||||
if (r >= 0) {
|
||||
ret->length = FAMILY_ADDRESS_SIZE(family);
|
||||
memcpy(ret->bytes, a.bytes, ret->length);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
max_len =
|
||||
expected_len == 0 ? INFINIBAND_ALEN :
|
||||
expected_len == SIZE_MAX ? HW_ADDR_MAX_SIZE : expected_len;
|
||||
sep = s[strspn(s, HEXDIGITS)];
|
||||
|
||||
if (sep == '.')
|
||||
field_size = 2;
|
||||
else if (IN_SET(sep, ':', '-'))
|
||||
field_size = 1;
|
||||
else
|
||||
return -EINVAL;
|
||||
|
||||
if (max_len % field_size != 0)
|
||||
return -EINVAL;
|
||||
|
||||
for (size_t i = 0; i < max_len / field_size; i++) {
|
||||
r = parse_hw_addr_one_field(&s, sep, field_size, bytes + i * field_size);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0) {
|
||||
len = (i + 1) * field_size;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (len == 0)
|
||||
return -EINVAL;
|
||||
|
||||
if (expected_len == 0) {
|
||||
if (!IN_SET(len, 4, 16, ETH_ALEN, INFINIBAND_ALEN))
|
||||
return -EINVAL;
|
||||
} else if (expected_len != SIZE_MAX) {
|
||||
if (len != expected_len)
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
ret->length = len;
|
||||
memcpy(ret->bytes, bytes, ret->length);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parse_ether_addr(const char *s, struct ether_addr *ret) {
|
||||
struct hw_addr_data a;
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
assert(ret);
|
||||
|
||||
s += strspn(s, WHITESPACE);
|
||||
sep = s[strspn(s, hex)];
|
||||
|
||||
if (sep == '.') {
|
||||
uint16_t shorts[3] = { 0 };
|
||||
|
||||
parse_fields(shorts);
|
||||
|
||||
if (s[pos] != '\0')
|
||||
return -EINVAL;
|
||||
|
||||
for (n = 0; n < ELEMENTSOF(shorts); n++) {
|
||||
ret->ether_addr_octet[2*n] = ((shorts[n] & (uint16_t)0xff00) >> 8);
|
||||
ret->ether_addr_octet[2*n + 1] = (shorts[n] & (uint16_t)0x00ff);
|
||||
}
|
||||
|
||||
} else if (IN_SET(sep, ':', '-')) {
|
||||
struct ether_addr out = ETHER_ADDR_NULL;
|
||||
|
||||
parse_fields(out.ether_addr_octet);
|
||||
|
||||
if (s[pos] != '\0')
|
||||
return -EINVAL;
|
||||
|
||||
for (n = 0; n < ELEMENTSOF(out.ether_addr_octet); n++)
|
||||
ret->ether_addr_octet[n] = out.ether_addr_octet[n];
|
||||
|
||||
} else
|
||||
return -EINVAL;
|
||||
r = parse_hw_addr_full(s, ETH_ALEN, &a);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
*ret = a.ether;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "hash-funcs.h"
|
||||
#include "in-addr-util.h"
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
|
||||
@@ -18,10 +19,18 @@ struct hw_addr_data {
|
||||
union {
|
||||
struct ether_addr ether;
|
||||
uint8_t infiniband[INFINIBAND_ALEN];
|
||||
struct in_addr in;
|
||||
struct in6_addr in6;
|
||||
uint8_t bytes[HW_ADDR_MAX_SIZE];
|
||||
};
|
||||
};
|
||||
|
||||
int parse_hw_addr_full(const char *s, size_t expected_len, struct hw_addr_data *ret);
|
||||
static inline int parse_hw_addr(const char *s, struct hw_addr_data *ret) {
|
||||
return parse_hw_addr_full(s, 0, ret);
|
||||
}
|
||||
int parse_ether_addr(const char *s, struct ether_addr *ret);
|
||||
|
||||
#define HW_ADDR_TO_STRING_MAX (3*HW_ADDR_MAX_SIZE)
|
||||
char* hw_addr_to_string(const struct hw_addr_data *addr, char buffer[HW_ADDR_TO_STRING_MAX]);
|
||||
|
||||
@@ -42,6 +51,7 @@ static inline bool hw_addr_is_null(const struct hw_addr_data *addr) {
|
||||
}
|
||||
|
||||
extern const struct hash_ops hw_addr_hash_ops;
|
||||
extern const struct hash_ops hw_addr_hash_ops_free;
|
||||
|
||||
#define ETHER_ADDR_FORMAT_STR "%02X%02X%02X%02X%02X%02X"
|
||||
#define ETHER_ADDR_FORMAT_VAL(x) (x).ether_addr_octet[0], (x).ether_addr_octet[1], (x).ether_addr_octet[2], (x).ether_addr_octet[3], (x).ether_addr_octet[4], (x).ether_addr_octet[5]
|
||||
@@ -83,6 +93,5 @@ static inline bool ether_addr_is_local(const struct ether_addr *addr) {
|
||||
return !FLAGS_SET(addr->ether_addr_octet[0], 0x02);
|
||||
}
|
||||
|
||||
int ether_addr_from_string(const char *s, struct ether_addr *ret);
|
||||
|
||||
extern const struct hash_ops ether_addr_hash_ops;
|
||||
extern const struct hash_ops ether_addr_hash_ops_free;
|
||||
|
||||
Reference in New Issue
Block a user