Commit ec7ef3ea authored by Jakub Kicinski's avatar Jakub Kicinski

Merge branch 'tcp-fix-bind-regression-and-more-tests'

Kuniyuki Iwashima says:

====================
tcp: Fix bind() regression and more tests.

bhash2 has not been well tested for IPV6_V6ONLY option.

This series fixes two regression around IPV6_V6ONLY, one of which
has been there since bhash2 introduction, and another is introduced
by a recent change.

Also, this series adds as many tests as possible to catch regression
easily.  The baseline is 28044fc1~ which is pre-bhash2 commit.

 Tested on 28044fc1~:
  # PASSED: 132 / 132 tests passed.
  # Totals: pass:132 fail:0 xfail:0 xpass:0 skip:0 error:0

 net.git:
  # FAILED: 125 / 132 tests passed.
  # Totals: pass:125 fail:7 xfail:0 xpass:0 skip:0 error:0

 With this series:
  # PASSED: 132 / 132 tests passed.
  # Totals: pass:132 fail:0 xfail:0 xpass:0 skip:0 error:0

v1: https://lore.kernel.org/netdev/20240325181923.48769-1-kuniyu@amazon.com/
====================

Link: https://lore.kernel.org/r/20240326204251.51301-1-kuniyu@amazon.comSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parents 17af4205 7679f096
...@@ -203,9 +203,16 @@ static bool __inet_bhash2_conflict(const struct sock *sk, struct sock *sk2, ...@@ -203,9 +203,16 @@ static bool __inet_bhash2_conflict(const struct sock *sk, struct sock *sk2,
kuid_t sk_uid, bool relax, kuid_t sk_uid, bool relax,
bool reuseport_cb_ok, bool reuseport_ok) bool reuseport_cb_ok, bool reuseport_ok)
{ {
if (sk->sk_family == AF_INET && ipv6_only_sock(sk2)) if (ipv6_only_sock(sk2)) {
if (sk->sk_family == AF_INET)
return false; return false;
#if IS_ENABLED(CONFIG_IPV6)
if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
return false;
#endif
}
return inet_bind_conflict(sk, sk2, sk_uid, relax, return inet_bind_conflict(sk, sk2, sk_uid, relax,
reuseport_cb_ok, reuseport_ok); reuseport_cb_ok, reuseport_ok);
} }
...@@ -287,6 +294,7 @@ static bool inet_bhash2_addr_any_conflict(const struct sock *sk, int port, int l ...@@ -287,6 +294,7 @@ static bool inet_bhash2_addr_any_conflict(const struct sock *sk, int port, int l
struct sock_reuseport *reuseport_cb; struct sock_reuseport *reuseport_cb;
struct inet_bind_hashbucket *head2; struct inet_bind_hashbucket *head2;
struct inet_bind2_bucket *tb2; struct inet_bind2_bucket *tb2;
bool conflict = false;
bool reuseport_cb_ok; bool reuseport_cb_ok;
rcu_read_lock(); rcu_read_lock();
...@@ -299,18 +307,20 @@ static bool inet_bhash2_addr_any_conflict(const struct sock *sk, int port, int l ...@@ -299,18 +307,20 @@ static bool inet_bhash2_addr_any_conflict(const struct sock *sk, int port, int l
spin_lock(&head2->lock); spin_lock(&head2->lock);
inet_bind_bucket_for_each(tb2, &head2->chain) inet_bind_bucket_for_each(tb2, &head2->chain) {
if (inet_bind2_bucket_match_addr_any(tb2, net, port, l3mdev, sk)) if (!inet_bind2_bucket_match_addr_any(tb2, net, port, l3mdev, sk))
break; continue;
if (tb2 && inet_bhash2_conflict(sk, tb2, uid, relax, reuseport_cb_ok, if (!inet_bhash2_conflict(sk, tb2, uid, relax, reuseport_cb_ok, reuseport_ok))
reuseport_ok)) { continue;
spin_unlock(&head2->lock);
return true; conflict = true;
break;
} }
spin_unlock(&head2->lock); spin_unlock(&head2->lock);
return false;
return conflict;
} }
/* /*
......
...@@ -6,7 +6,9 @@ ...@@ -6,7 +6,9 @@
#include "../kselftest_harness.h" #include "../kselftest_harness.h"
struct in6_addr in6addr_v4mapped_any = { static const __u32 in4addr_any = INADDR_ANY;
static const __u32 in4addr_loopback = INADDR_LOOPBACK;
static const struct in6_addr in6addr_v4mapped_any = {
.s6_addr = { .s6_addr = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
...@@ -14,8 +16,7 @@ struct in6_addr in6addr_v4mapped_any = { ...@@ -14,8 +16,7 @@ struct in6_addr in6addr_v4mapped_any = {
0, 0, 0, 0 0, 0, 0, 0
} }
}; };
static const struct in6_addr in6addr_v4mapped_loopback = {
struct in6_addr in6addr_v4mapped_loopback = {
.s6_addr = { .s6_addr = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
...@@ -24,137 +25,785 @@ struct in6_addr in6addr_v4mapped_loopback = { ...@@ -24,137 +25,785 @@ struct in6_addr in6addr_v4mapped_loopback = {
} }
}; };
#define NR_SOCKETS 8
FIXTURE(bind_wildcard) FIXTURE(bind_wildcard)
{ {
int fd[NR_SOCKETS];
socklen_t addrlen[NR_SOCKETS];
union {
struct sockaddr addr;
struct sockaddr_in addr4; struct sockaddr_in addr4;
struct sockaddr_in6 addr6; struct sockaddr_in6 addr6;
} addr[NR_SOCKETS];
}; };
FIXTURE_VARIANT(bind_wildcard) FIXTURE_VARIANT(bind_wildcard)
{ {
const __u32 addr4_const; sa_family_t family[2];
const struct in6_addr *addr6_const; const void *addr[2];
int expected_errno; bool ipv6_only[2];
/* 6 bind() calls below follow two bind() for the defined 2 addresses:
*
* 0.0.0.0
* 127.0.0.1
* ::
* ::1
* ::ffff:0.0.0.0
* ::ffff:127.0.0.1
*/
int expected_errno[NR_SOCKETS];
int expected_reuse_errno[NR_SOCKETS];
};
/* (IPv4, IPv4) */
FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v4_local)
{
.family = {AF_INET, AF_INET},
.addr = {&in4addr_any, &in4addr_loopback},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v4_any)
{
.family = {AF_INET, AF_INET},
.addr = {&in4addr_loopback, &in4addr_any},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
}; };
/* (IPv4, IPv6) */
FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_any) FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_any)
{ {
.addr4_const = INADDR_ANY, .family = {AF_INET, AF_INET6},
.addr6_const = &in6addr_any, .addr = {&in4addr_any, &in6addr_any},
.expected_errno = EADDRINUSE, .expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_any_only)
{
.family = {AF_INET, AF_INET6},
.addr = {&in4addr_any, &in6addr_any},
.ipv6_only = {false, true},
.expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
}; };
FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_local) FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_local)
{ {
.addr4_const = INADDR_ANY, .family = {AF_INET, AF_INET6},
.addr6_const = &in6addr_loopback, .addr = {&in4addr_any, &in6addr_loopback},
.expected_errno = 0, .expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
}; };
FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_v4mapped_any) FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_v4mapped_any)
{ {
.addr4_const = INADDR_ANY, .family = {AF_INET, AF_INET6},
.addr6_const = &in6addr_v4mapped_any, .addr = {&in4addr_any, &in6addr_v4mapped_any},
.expected_errno = EADDRINUSE, .expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
}; };
FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_v4mapped_local) FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_v4mapped_local)
{ {
.addr4_const = INADDR_ANY, .family = {AF_INET, AF_INET6},
.addr6_const = &in6addr_v4mapped_loopback, .addr = {&in4addr_any, &in6addr_v4mapped_loopback},
.expected_errno = EADDRINUSE, .expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
}; };
FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_any) FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_any)
{ {
.addr4_const = INADDR_LOOPBACK, .family = {AF_INET, AF_INET6},
.addr6_const = &in6addr_any, .addr = {&in4addr_loopback, &in6addr_any},
.expected_errno = EADDRINUSE, .expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_any_only)
{
.family = {AF_INET, AF_INET6},
.addr = {&in4addr_loopback, &in6addr_any},
.ipv6_only = {false, true},
.expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
}; };
FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_local) FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_local)
{ {
.addr4_const = INADDR_LOOPBACK, .family = {AF_INET, AF_INET6},
.addr6_const = &in6addr_loopback, .addr = {&in4addr_loopback, &in6addr_loopback},
.expected_errno = 0, .expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
}; };
FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_v4mapped_any) FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_v4mapped_any)
{ {
.addr4_const = INADDR_LOOPBACK, .family = {AF_INET, AF_INET6},
.addr6_const = &in6addr_v4mapped_any, .addr = {&in4addr_loopback, &in6addr_v4mapped_any},
.expected_errno = EADDRINUSE, .expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
}; };
FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_v4mapped_local) FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_v4mapped_local)
{ {
.addr4_const = INADDR_LOOPBACK, .family = {AF_INET, AF_INET6},
.addr6_const = &in6addr_v4mapped_loopback, .addr = {&in4addr_loopback, &in6addr_v4mapped_loopback},
.expected_errno = EADDRINUSE, .expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
};
/* (IPv6, IPv4) */
FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v4_any)
{
.family = {AF_INET6, AF_INET},
.addr = {&in6addr_any, &in4addr_any},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v4_any)
{
.family = {AF_INET6, AF_INET},
.addr = {&in6addr_any, &in4addr_any},
.ipv6_only = {true, false},
.expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v4_local)
{
.family = {AF_INET6, AF_INET},
.addr = {&in6addr_any, &in4addr_loopback},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v4_local)
{
.family = {AF_INET6, AF_INET},
.addr = {&in6addr_any, &in4addr_loopback},
.ipv6_only = {true, false},
.expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v4_any)
{
.family = {AF_INET6, AF_INET},
.addr = {&in6addr_loopback, &in4addr_any},
.expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v4_local)
{
.family = {AF_INET6, AF_INET},
.addr = {&in6addr_loopback, &in4addr_loopback},
.expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v4_any)
{
.family = {AF_INET6, AF_INET},
.addr = {&in6addr_v4mapped_any, &in4addr_any},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v4_local)
{
.family = {AF_INET6, AF_INET},
.addr = {&in6addr_v4mapped_any, &in4addr_loopback},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_local_v4_any)
{
.family = {AF_INET6, AF_INET},
.addr = {&in6addr_v4mapped_loopback, &in4addr_any},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_local_v4_local)
{
.family = {AF_INET6, AF_INET},
.addr = {&in6addr_v4mapped_loopback, &in4addr_loopback},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
};
/* (IPv6, IPv6) */
FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_any)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_any, &in6addr_any},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v6_any)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_any, &in6addr_any},
.ipv6_only = {true, false},
.expected_errno = {0, EADDRINUSE,
0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_any_only)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_any, &in6addr_any},
.ipv6_only = {false, true},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v6_any_only)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_any, &in6addr_any},
.ipv6_only = {true, true},
.expected_errno = {0, EADDRINUSE,
0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_local)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_any, &in6addr_loopback},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v6_local)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_any, &in6addr_loopback},
.ipv6_only = {true, false},
.expected_errno = {0, EADDRINUSE,
0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_v4mapped_any)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_any, &in6addr_v4mapped_any},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v6_v4mapped_any)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_any, &in6addr_v4mapped_any},
.ipv6_only = {true, false},
.expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
}; };
FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_v4mapped_local)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_any, &in6addr_v4mapped_loopback},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v6_v4mapped_local)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_any, &in6addr_v4mapped_loopback},
.ipv6_only = {true, false},
.expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_any)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_loopback, &in6addr_any},
.expected_errno = {0, EADDRINUSE,
0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_any_only)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_loopback, &in6addr_any},
.ipv6_only = {false, true},
.expected_errno = {0, EADDRINUSE,
0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_v4mapped_any)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_loopback, &in6addr_v4mapped_any},
.expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_v4mapped_local)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_loopback, &in6addr_v4mapped_loopback},
.expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_any)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_v4mapped_any, &in6addr_any},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_any_only)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_v4mapped_any, &in6addr_any},
.ipv6_only = {false, true},
.expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_local)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_v4mapped_any, &in6addr_loopback},
.expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_v4mapped_local)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_v4mapped_any, &in6addr_v4mapped_loopback},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_any)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_v4mapped_loopback, &in6addr_any},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_any_only)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_v4mapped_loopback, &in6addr_any},
.ipv6_only = {false, true},
.expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_local)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_v4mapped_loopback, &in6addr_loopback},
.expected_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, EADDRINUSE},
};
FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_v4mapped_any)
{
.family = {AF_INET6, AF_INET6},
.addr = {&in6addr_v4mapped_loopback, &in6addr_v4mapped_any},
.expected_errno = {0, EADDRINUSE,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
.expected_reuse_errno = {0, 0,
EADDRINUSE, EADDRINUSE,
EADDRINUSE, 0,
EADDRINUSE, EADDRINUSE},
};
static void setup_addr(FIXTURE_DATA(bind_wildcard) *self, int i,
int family, const void *addr_const)
{
if (family == AF_INET) {
struct sockaddr_in *addr4 = &self->addr[i].addr4;
const __u32 *addr4_const = addr_const;
addr4->sin_family = AF_INET;
addr4->sin_port = htons(0);
addr4->sin_addr.s_addr = htonl(*addr4_const);
self->addrlen[i] = sizeof(struct sockaddr_in);
} else {
struct sockaddr_in6 *addr6 = &self->addr[i].addr6;
const struct in6_addr *addr6_const = addr_const;
addr6->sin6_family = AF_INET6;
addr6->sin6_port = htons(0);
addr6->sin6_addr = *addr6_const;
self->addrlen[i] = sizeof(struct sockaddr_in6);
}
}
FIXTURE_SETUP(bind_wildcard) FIXTURE_SETUP(bind_wildcard)
{ {
self->addr4.sin_family = AF_INET; setup_addr(self, 0, variant->family[0], variant->addr[0]);
self->addr4.sin_port = htons(0); setup_addr(self, 1, variant->family[1], variant->addr[1]);
self->addr4.sin_addr.s_addr = htonl(variant->addr4_const);
setup_addr(self, 2, AF_INET, &in4addr_any);
setup_addr(self, 3, AF_INET, &in4addr_loopback);
self->addr6.sin6_family = AF_INET6; setup_addr(self, 4, AF_INET6, &in6addr_any);
self->addr6.sin6_port = htons(0); setup_addr(self, 5, AF_INET6, &in6addr_loopback);
self->addr6.sin6_addr = *variant->addr6_const; setup_addr(self, 6, AF_INET6, &in6addr_v4mapped_any);
setup_addr(self, 7, AF_INET6, &in6addr_v4mapped_loopback);
} }
FIXTURE_TEARDOWN(bind_wildcard) FIXTURE_TEARDOWN(bind_wildcard)
{ {
int i;
for (i = 0; i < NR_SOCKETS; i++)
close(self->fd[i]);
} }
void bind_sockets(struct __test_metadata *_metadata, void bind_socket(struct __test_metadata *_metadata,
FIXTURE_DATA(bind_wildcard) *self, FIXTURE_DATA(bind_wildcard) *self,
int expected_errno, const FIXTURE_VARIANT(bind_wildcard) *variant,
struct sockaddr *addr1, socklen_t addrlen1, int i, int reuse)
struct sockaddr *addr2, socklen_t addrlen2)
{ {
int fd[2];
int ret; int ret;
fd[0] = socket(addr1->sa_family, SOCK_STREAM, 0); self->fd[i] = socket(self->addr[i].addr.sa_family, SOCK_STREAM, 0);
ASSERT_GT(fd[0], 0); ASSERT_GT(self->fd[i], 0);
ret = bind(fd[0], addr1, addrlen1); if (i < 2 && variant->ipv6_only[i]) {
ret = setsockopt(self->fd[i], SOL_IPV6, IPV6_V6ONLY, &(int){1}, sizeof(int));
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
}
ret = getsockname(fd[0], addr1, &addrlen1); if (i < 2 && reuse) {
ret = setsockopt(self->fd[i], SOL_SOCKET, reuse, &(int){1}, sizeof(int));
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
}
((struct sockaddr_in *)addr2)->sin_port = ((struct sockaddr_in *)addr1)->sin_port; self->addr[i].addr4.sin_port = self->addr[0].addr4.sin_port;
fd[1] = socket(addr2->sa_family, SOCK_STREAM, 0); ret = bind(self->fd[i], &self->addr[i].addr, self->addrlen[i]);
ASSERT_GT(fd[1], 0);
ret = bind(fd[1], addr2, addrlen2); if (reuse) {
if (expected_errno) { if (variant->expected_reuse_errno[i]) {
ASSERT_EQ(ret, -1); ASSERT_EQ(ret, -1);
ASSERT_EQ(errno, expected_errno); ASSERT_EQ(errno, variant->expected_reuse_errno[i]);
} else { } else {
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
} }
} else {
if (variant->expected_errno[i]) {
ASSERT_EQ(ret, -1);
ASSERT_EQ(errno, variant->expected_errno[i]);
} else {
ASSERT_EQ(ret, 0);
}
}
close(fd[1]); if (i == 0) {
close(fd[0]); ret = getsockname(self->fd[0], &self->addr[0].addr, &self->addrlen[0]);
ASSERT_EQ(ret, 0);
}
}
TEST_F(bind_wildcard, plain)
{
int i;
for (i = 0; i < NR_SOCKETS; i++)
bind_socket(_metadata, self, variant, i, 0);
} }
TEST_F(bind_wildcard, v4_v6) TEST_F(bind_wildcard, reuseaddr)
{ {
bind_sockets(_metadata, self, variant->expected_errno, int i;
(struct sockaddr *)&self->addr4, sizeof(self->addr4),
(struct sockaddr *)&self->addr6, sizeof(self->addr6)); for (i = 0; i < NR_SOCKETS; i++)
bind_socket(_metadata, self, variant, i, SO_REUSEADDR);
} }
TEST_F(bind_wildcard, v6_v4) TEST_F(bind_wildcard, reuseport)
{ {
bind_sockets(_metadata, self, variant->expected_errno, int i;
(struct sockaddr *)&self->addr6, sizeof(self->addr6),
(struct sockaddr *)&self->addr4, sizeof(self->addr4)); for (i = 0; i < NR_SOCKETS; i++)
bind_socket(_metadata, self, variant, i, SO_REUSEPORT);
} }
TEST_HARNESS_MAIN TEST_HARNESS_MAIN
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment