tcp: Hash IPv4 and IPv4-mapped-IPv6 addresses the same
In the tcp_conn structure, we represent the address with an inany_addr which could be an IPv4 or IPv6 address. However, we have different paths which will calculate different hashes for IPv4 and equivalent IPv4-mapped IPv6 addresses. This will cause problems for some future changes. Make the hash function work the same for these two cases, by taking an inany_addr directly. Since this represents IPv4 and IPv4-mapped IPv6 addresses the same way, it will trivially hash the same for both cases. Callers are changed to construct an inany_addr from whatever they have. Some of that will be elided in later changes. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
parent
ca69c3f196
commit
685f97b8de
2 changed files with 19 additions and 34 deletions
|
@ -104,6 +104,7 @@
|
||||||
*
|
*
|
||||||
* Return: the 64-bit hash output
|
* Return: the 64-bit hash output
|
||||||
*/
|
*/
|
||||||
|
/* cppcheck-suppress unusedFunction */
|
||||||
uint64_t siphash_8b(const uint8_t *in, const uint64_t *k)
|
uint64_t siphash_8b(const uint8_t *in, const uint64_t *k)
|
||||||
{
|
{
|
||||||
PREAMBLE(8);
|
PREAMBLE(8);
|
||||||
|
|
52
tcp.c
52
tcp.c
|
@ -1205,8 +1205,7 @@ static int tcp_hash_match(const struct tcp_tap_conn *conn,
|
||||||
/**
|
/**
|
||||||
* tcp_hash() - Calculate hash value for connection given address and ports
|
* tcp_hash() - Calculate hash value for connection given address and ports
|
||||||
* @c: Execution context
|
* @c: Execution context
|
||||||
* @af: Address family, AF_INET or AF_INET6
|
* @addr: Remote address
|
||||||
* @addr: Remote address, pointer to in_addr or in6_addr
|
|
||||||
* @tap_port: tap-facing port
|
* @tap_port: tap-facing port
|
||||||
* @sock_port: Socket-facing port
|
* @sock_port: Socket-facing port
|
||||||
*
|
*
|
||||||
|
@ -1215,32 +1214,19 @@ static int tcp_hash_match(const struct tcp_tap_conn *conn,
|
||||||
#if TCP_HASH_NOINLINE
|
#if TCP_HASH_NOINLINE
|
||||||
__attribute__((__noinline__)) /* See comment in Makefile */
|
__attribute__((__noinline__)) /* See comment in Makefile */
|
||||||
#endif
|
#endif
|
||||||
static unsigned int tcp_hash(const struct ctx *c, int af, const void *addr,
|
static unsigned int tcp_hash(const struct ctx *c, const union inany_addr *addr,
|
||||||
in_port_t tap_port, in_port_t sock_port)
|
in_port_t tap_port, in_port_t sock_port)
|
||||||
{
|
{
|
||||||
|
struct {
|
||||||
|
union inany_addr addr;
|
||||||
|
in_port_t tap_port;
|
||||||
|
in_port_t sock_port;
|
||||||
|
} __attribute__((__packed__)) in = {
|
||||||
|
*addr, tap_port, sock_port
|
||||||
|
};
|
||||||
uint64_t b = 0;
|
uint64_t b = 0;
|
||||||
|
|
||||||
if (af == AF_INET) {
|
b = siphash_20b((uint8_t *)&in, c->tcp.hash_secret);
|
||||||
struct {
|
|
||||||
struct in_addr addr;
|
|
||||||
in_port_t tap_port;
|
|
||||||
in_port_t sock_port;
|
|
||||||
} __attribute__((__packed__)) in = {
|
|
||||||
*(struct in_addr *)addr, tap_port, sock_port,
|
|
||||||
};
|
|
||||||
|
|
||||||
b = siphash_8b((uint8_t *)&in, c->tcp.hash_secret);
|
|
||||||
} else if (af == AF_INET6) {
|
|
||||||
struct {
|
|
||||||
struct in6_addr addr;
|
|
||||||
in_port_t tap_port;
|
|
||||||
in_port_t sock_port;
|
|
||||||
} __attribute__((__packed__)) in = {
|
|
||||||
*(struct in6_addr *)addr, tap_port, sock_port,
|
|
||||||
};
|
|
||||||
|
|
||||||
b = siphash_20b((uint8_t *)&in, c->tcp.hash_secret);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (unsigned int)(b % TCP_HASH_TABLE_SIZE);
|
return (unsigned int)(b % TCP_HASH_TABLE_SIZE);
|
||||||
}
|
}
|
||||||
|
@ -1255,14 +1241,7 @@ static unsigned int tcp_hash(const struct ctx *c, int af, const void *addr,
|
||||||
static unsigned int tcp_conn_hash(const struct ctx *c,
|
static unsigned int tcp_conn_hash(const struct ctx *c,
|
||||||
const struct tcp_tap_conn *conn)
|
const struct tcp_tap_conn *conn)
|
||||||
{
|
{
|
||||||
const struct in_addr *a4 = inany_v4(&conn->addr);
|
return tcp_hash(c, &conn->addr, conn->tap_port, conn->sock_port);
|
||||||
|
|
||||||
if (a4)
|
|
||||||
return tcp_hash(c, AF_INET, a4,
|
|
||||||
conn->tap_port, conn->sock_port);
|
|
||||||
else
|
|
||||||
return tcp_hash(c, AF_INET6, &conn->addr.a6,
|
|
||||||
conn->tap_port, conn->sock_port);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1275,9 +1254,11 @@ static unsigned int tcp_conn_hash(const struct ctx *c,
|
||||||
static void tcp_hash_insert(const struct ctx *c, struct tcp_tap_conn *conn,
|
static void tcp_hash_insert(const struct ctx *c, struct tcp_tap_conn *conn,
|
||||||
int af, const void *addr)
|
int af, const void *addr)
|
||||||
{
|
{
|
||||||
|
union inany_addr aany;
|
||||||
int b;
|
int b;
|
||||||
|
|
||||||
b = tcp_hash(c, af, addr, conn->tap_port, conn->sock_port);
|
inany_from_af(&aany, af, addr);
|
||||||
|
b = tcp_hash(c, &aany, conn->tap_port, conn->sock_port);
|
||||||
conn->next_index = tc_hash[b] ? CONN_IDX(tc_hash[b]) : -1;
|
conn->next_index = tc_hash[b] ? CONN_IDX(tc_hash[b]) : -1;
|
||||||
tc_hash[b] = conn;
|
tc_hash[b] = conn;
|
||||||
|
|
||||||
|
@ -1357,9 +1338,12 @@ static struct tcp_tap_conn *tcp_hash_lookup(const struct ctx *c,
|
||||||
in_port_t tap_port,
|
in_port_t tap_port,
|
||||||
in_port_t sock_port)
|
in_port_t sock_port)
|
||||||
{
|
{
|
||||||
int b = tcp_hash(c, af, addr, tap_port, sock_port);
|
union inany_addr aany;
|
||||||
struct tcp_tap_conn *conn;
|
struct tcp_tap_conn *conn;
|
||||||
|
int b;
|
||||||
|
|
||||||
|
inany_from_af(&aany, af, addr);
|
||||||
|
b = tcp_hash(c, &aany, tap_port, sock_port);
|
||||||
for (conn = tc_hash[b]; conn; conn = conn_at_idx(conn->next_index)) {
|
for (conn = tc_hash[b]; conn; conn = conn_at_idx(conn->next_index)) {
|
||||||
if (tcp_hash_match(conn, af, addr, tap_port, sock_port))
|
if (tcp_hash_match(conn, af, addr, tap_port, sock_port))
|
||||||
return conn;
|
return conn;
|
||||||
|
|
Loading…
Reference in a new issue