Fix wildcard bind for raw socket.

Fixes #3334

PiperOrigin-RevId: 322846384
This commit is contained in:
Bhasker Hariharan
2020-07-23 12:54:12 -07:00
committed by gVisor bot
parent 6f7f739967
commit 20b556e625
2 changed files with 22 additions and 1 deletions
+1 -1
View File
@@ -456,7 +456,7 @@ func (e *endpoint) Bind(addr tcpip.FullAddress) *tcpip.Error {
defer e.mu.Unlock()
// If a local address was specified, verify that it's valid.
if e.stack.CheckLocalAddress(addr.NIC, e.NetProto, addr.Addr) == 0 {
if len(addr.Addr) != 0 && e.stack.CheckLocalAddress(addr.NIC, e.NetProto, addr.Addr) == 0 {
return tcpip.ErrBadLocalAddress
}
+21
View File
@@ -262,6 +262,27 @@ TEST_P(RawSocketTest, SendWithoutConnectFails) {
SyscallFailsWithErrno(EDESTADDRREQ));
}
// Wildcard Bind.
TEST_P(RawSocketTest, BindToWildcard) {
SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_NET_RAW)));
struct sockaddr_storage addr;
addr = {};
// We don't set ports because raw sockets don't have a notion of ports.
if (Family() == AF_INET) {
struct sockaddr_in* sin = reinterpret_cast<struct sockaddr_in*>(&addr);
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = htonl(INADDR_ANY);
} else {
struct sockaddr_in6* sin6 = reinterpret_cast<struct sockaddr_in6*>(&addr);
sin6->sin6_family = AF_INET6;
sin6->sin6_addr = in6addr_any;
}
ASSERT_THAT(bind(s_, reinterpret_cast<struct sockaddr*>(&addr_), AddrLen()),
SyscallSucceeds());
}
// Bind to localhost.
TEST_P(RawSocketTest, BindToLocalhost) {
SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_NET_RAW)));