2020-07-10 05:37:11 +00:00
|
|
|
// Copyright 2020 The gVisor Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package tcp_synsent_reset_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/header"
|
|
|
|
"gvisor.dev/gvisor/test/packetimpact/testbench"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
testbench.RegisterFlags(flag.CommandLine)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTCPSynSentUnreachable verifies that TCP connections fail immediately when
|
|
|
|
// an ICMP destination unreachable message is sent in response to the inital
|
|
|
|
// SYN.
|
|
|
|
func TestTCPSynSentUnreachable(t *testing.T) {
|
|
|
|
// Create the DUT and connection.
|
|
|
|
dut := testbench.NewDUT(t)
|
|
|
|
defer dut.TearDown()
|
2020-07-27 13:37:11 +00:00
|
|
|
clientFD, clientPort := dut.CreateBoundSocket(t, unix.SOCK_STREAM|unix.SOCK_NONBLOCK, unix.IPPROTO_TCP, net.ParseIP(testbench.RemoteIPv4))
|
2020-07-10 05:37:11 +00:00
|
|
|
port := uint16(9001)
|
|
|
|
conn := testbench.NewTCPIPv4(t, testbench.TCP{SrcPort: &port, DstPort: &clientPort}, testbench.TCP{SrcPort: &clientPort, DstPort: &port})
|
2020-07-27 13:37:11 +00:00
|
|
|
defer conn.Close(t)
|
2020-07-10 05:37:11 +00:00
|
|
|
|
|
|
|
// Bring the DUT to SYN-SENT state with a non-blocking connect.
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testbench.RPCTimeout)
|
|
|
|
defer cancel()
|
|
|
|
sa := unix.SockaddrInet4{Port: int(port)}
|
|
|
|
copy(sa.Addr[:], net.IP(net.ParseIP(testbench.LocalIPv4)).To4())
|
2020-07-27 13:37:11 +00:00
|
|
|
if _, err := dut.ConnectWithErrno(ctx, t, clientFD, &sa); err != syscall.Errno(unix.EINPROGRESS) {
|
2020-07-10 05:37:11 +00:00
|
|
|
t.Errorf("expected connect to fail with EINPROGRESS, but got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the SYN.
|
2020-07-27 13:37:11 +00:00
|
|
|
tcpLayers, err := conn.ExpectData(t, &testbench.TCP{Flags: testbench.Uint8(header.TCPFlagSyn)}, nil, time.Second)
|
2020-07-10 05:37:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected SYN: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a host unreachable message.
|
|
|
|
rawConn := (*testbench.Connection)(&conn)
|
2020-07-27 13:37:11 +00:00
|
|
|
layers := rawConn.CreateFrame(t, nil)
|
2020-07-10 05:37:11 +00:00
|
|
|
layers = layers[:len(layers)-1]
|
|
|
|
const ipLayer = 1
|
|
|
|
const tcpLayer = ipLayer + 1
|
|
|
|
ip, ok := tcpLayers[ipLayer].(*testbench.IPv4)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected %s to be IPv4", tcpLayers[ipLayer])
|
|
|
|
}
|
|
|
|
tcp, ok := tcpLayers[tcpLayer].(*testbench.TCP)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected %s to be TCP", tcpLayers[tcpLayer])
|
|
|
|
}
|
|
|
|
var icmpv4 testbench.ICMPv4 = testbench.ICMPv4{Type: testbench.ICMPv4Type(header.ICMPv4DstUnreachable), Code: testbench.Uint8(header.ICMPv4HostUnreachable)}
|
|
|
|
layers = append(layers, &icmpv4, ip, tcp)
|
2020-07-27 13:37:11 +00:00
|
|
|
rawConn.SendFrameStateless(t, layers)
|
2020-07-10 05:37:11 +00:00
|
|
|
|
2020-07-27 13:37:11 +00:00
|
|
|
if _, err = dut.ConnectWithErrno(ctx, t, clientFD, &sa); err != syscall.Errno(unix.EHOSTUNREACH) {
|
2020-07-10 05:37:11 +00:00
|
|
|
t.Errorf("expected connect to fail with EHOSTUNREACH, but got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTCPSynSentUnreachable6 verifies that TCP connections fail immediately when
|
|
|
|
// an ICMP destination unreachable message is sent in response to the inital
|
|
|
|
// SYN.
|
|
|
|
func TestTCPSynSentUnreachable6(t *testing.T) {
|
|
|
|
// Create the DUT and connection.
|
|
|
|
dut := testbench.NewDUT(t)
|
|
|
|
defer dut.TearDown()
|
2020-07-27 13:37:11 +00:00
|
|
|
clientFD, clientPort := dut.CreateBoundSocket(t, unix.SOCK_STREAM|unix.SOCK_NONBLOCK, unix.IPPROTO_TCP, net.ParseIP(testbench.RemoteIPv6))
|
2020-07-10 05:37:11 +00:00
|
|
|
conn := testbench.NewTCPIPv6(t, testbench.TCP{DstPort: &clientPort}, testbench.TCP{SrcPort: &clientPort})
|
2020-07-27 13:37:11 +00:00
|
|
|
defer conn.Close(t)
|
2020-07-10 05:37:11 +00:00
|
|
|
|
|
|
|
// Bring the DUT to SYN-SENT state with a non-blocking connect.
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testbench.RPCTimeout)
|
|
|
|
defer cancel()
|
|
|
|
sa := unix.SockaddrInet6{
|
|
|
|
Port: int(conn.SrcPort()),
|
|
|
|
ZoneId: uint32(testbench.RemoteInterfaceID),
|
|
|
|
}
|
|
|
|
copy(sa.Addr[:], net.IP(net.ParseIP(testbench.LocalIPv6)).To16())
|
2020-07-27 13:37:11 +00:00
|
|
|
if _, err := dut.ConnectWithErrno(ctx, t, clientFD, &sa); err != syscall.Errno(unix.EINPROGRESS) {
|
2020-07-10 05:37:11 +00:00
|
|
|
t.Errorf("expected connect to fail with EINPROGRESS, but got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the SYN.
|
2020-07-27 13:37:11 +00:00
|
|
|
tcpLayers, err := conn.ExpectData(t, &testbench.TCP{Flags: testbench.Uint8(header.TCPFlagSyn)}, nil, time.Second)
|
2020-07-10 05:37:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected SYN: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a host unreachable message.
|
|
|
|
rawConn := (*testbench.Connection)(&conn)
|
2020-07-27 13:37:11 +00:00
|
|
|
layers := rawConn.CreateFrame(t, nil)
|
2020-07-10 05:37:11 +00:00
|
|
|
layers = layers[:len(layers)-1]
|
|
|
|
const ipLayer = 1
|
|
|
|
const tcpLayer = ipLayer + 1
|
|
|
|
ip, ok := tcpLayers[ipLayer].(*testbench.IPv6)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected %s to be IPv6", tcpLayers[ipLayer])
|
|
|
|
}
|
|
|
|
tcp, ok := tcpLayers[tcpLayer].(*testbench.TCP)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected %s to be TCP", tcpLayers[tcpLayer])
|
|
|
|
}
|
|
|
|
var icmpv6 testbench.ICMPv6 = testbench.ICMPv6{
|
|
|
|
Type: testbench.ICMPv6Type(header.ICMPv6DstUnreachable),
|
|
|
|
Code: testbench.Uint8(header.ICMPv6NetworkUnreachable),
|
|
|
|
// Per RFC 4443 3.1, the payload contains 4 zeroed bytes.
|
|
|
|
Payload: []byte{0, 0, 0, 0},
|
|
|
|
}
|
|
|
|
layers = append(layers, &icmpv6, ip, tcp)
|
2020-07-27 13:37:11 +00:00
|
|
|
rawConn.SendFrameStateless(t, layers)
|
2020-07-10 05:37:11 +00:00
|
|
|
|
2020-07-27 13:37:11 +00:00
|
|
|
if _, err = dut.ConnectWithErrno(ctx, t, clientFD, &sa); err != syscall.Errno(unix.ENETUNREACH) {
|
2020-07-10 05:37:11 +00:00
|
|
|
t.Errorf("expected connect to fail with ENETUNREACH, but got %v", err)
|
|
|
|
}
|
|
|
|
}
|