gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/tcpip/transport/tcp/test/e2e/tcp_test.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tcp_test
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"math"
    22  	"os"
    23  	"strings"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/google/go-cmp/cmp"
    28  	"gvisor.dev/gvisor/pkg/buffer"
    29  	"gvisor.dev/gvisor/pkg/rand"
    30  	"gvisor.dev/gvisor/pkg/refs"
    31  	"gvisor.dev/gvisor/pkg/sync"
    32  	"gvisor.dev/gvisor/pkg/tcpip"
    33  	"gvisor.dev/gvisor/pkg/tcpip/checker"
    34  	"gvisor.dev/gvisor/pkg/tcpip/faketime"
    35  	"gvisor.dev/gvisor/pkg/tcpip/header"
    36  	"gvisor.dev/gvisor/pkg/tcpip/link/loopback"
    37  	"gvisor.dev/gvisor/pkg/tcpip/link/sniffer"
    38  	"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
    39  	"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
    40  	"gvisor.dev/gvisor/pkg/tcpip/seqnum"
    41  	"gvisor.dev/gvisor/pkg/tcpip/stack"
    42  	tcpiptestutil "gvisor.dev/gvisor/pkg/tcpip/testutil"
    43  	"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
    44  	"gvisor.dev/gvisor/pkg/tcpip/transport/tcp/test/e2e"
    45  	"gvisor.dev/gvisor/pkg/tcpip/transport/tcp/testing/context"
    46  	"gvisor.dev/gvisor/pkg/test/testutil"
    47  	"gvisor.dev/gvisor/pkg/waiter"
    48  )
    49  
    50  // endpointTester provides helper functions to test a tcpip.Endpoint.
    51  type endpointTester struct {
    52  	ep tcpip.Endpoint
    53  }
    54  
    55  // CheckReadError issues a read to the endpoint and checking for an error.
    56  func (e *endpointTester) CheckReadError(t *testing.T, want tcpip.Error) {
    57  	t.Helper()
    58  	res, got := e.ep.Read(ioutil.Discard, tcpip.ReadOptions{})
    59  	if got != want {
    60  		t.Fatalf("ep.Read = %s, want %s", got, want)
    61  	}
    62  	if diff := cmp.Diff(tcpip.ReadResult{}, res); diff != "" {
    63  		t.Errorf("ep.Read: unexpected non-zero result (-want +got):\n%s", diff)
    64  	}
    65  }
    66  
    67  // CheckRead issues a read to the endpoint and checking for a success, returning
    68  // the data read.
    69  func (e *endpointTester) CheckRead(t *testing.T) []byte {
    70  	t.Helper()
    71  	var buf bytes.Buffer
    72  	res, err := e.ep.Read(&buf, tcpip.ReadOptions{})
    73  	if err != nil {
    74  		t.Fatalf("ep.Read = _, %s; want _, nil", err)
    75  	}
    76  	if diff := cmp.Diff(tcpip.ReadResult{
    77  		Count: buf.Len(),
    78  		Total: buf.Len(),
    79  	}, res, checker.IgnoreCmpPath("ControlMessages")); diff != "" {
    80  		t.Errorf("ep.Read: unexpected result (-want +got):\n%s", diff)
    81  	}
    82  	return buf.Bytes()
    83  }
    84  
    85  // CheckReadFull reads from the endpoint for exactly count bytes.
    86  func (e *endpointTester) CheckReadFull(t *testing.T, count int, notifyRead <-chan struct{}, timeout time.Duration) []byte {
    87  	t.Helper()
    88  	var buf bytes.Buffer
    89  	w := tcpip.LimitedWriter{
    90  		W: &buf,
    91  		N: int64(count),
    92  	}
    93  	for w.N != 0 {
    94  		_, err := e.ep.Read(&w, tcpip.ReadOptions{})
    95  		if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
    96  			// Wait for receive to be notified.
    97  			select {
    98  			case <-notifyRead:
    99  			case <-time.After(timeout):
   100  				t.Fatalf("Timed out waiting for data to arrive")
   101  			}
   102  			continue
   103  		} else if err != nil {
   104  			t.Fatalf("ep.Read = _, %s; want _, nil", err)
   105  		}
   106  	}
   107  	return buf.Bytes()
   108  }
   109  
   110  func TestGiveUpConnect(t *testing.T) {
   111  	c := context.New(t, e2e.DefaultMTU)
   112  	defer c.Cleanup()
   113  
   114  	var wq waiter.Queue
   115  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq)
   116  	if err != nil {
   117  		t.Fatalf("NewEndpoint failed: %s", err)
   118  	}
   119  
   120  	// Register for notification, then start connection attempt.
   121  	waitEntry, notifyCh := waiter.NewChannelEntry(waiter.EventHUp)
   122  	wq.EventRegister(&waitEntry)
   123  	defer wq.EventUnregister(&waitEntry)
   124  
   125  	{
   126  		err := ep.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort})
   127  		if d := cmp.Diff(&tcpip.ErrConnectStarted{}, err); d != "" {
   128  			t.Fatalf("ep.Connect(...) mismatch (-want +got):\n%s", d)
   129  		}
   130  	}
   131  
   132  	// Close the connection, wait for completion.
   133  	ep.Close()
   134  
   135  	// Wait for ep to become writable.
   136  	<-notifyCh
   137  
   138  	// Call Connect again to retrieve the handshake failure status
   139  	// and stats updates.
   140  	{
   141  		err := ep.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort})
   142  		if d := cmp.Diff(&tcpip.ErrAborted{}, err); d != "" {
   143  			t.Fatalf("ep.Connect(...) mismatch (-want +got):\n%s", d)
   144  		}
   145  	}
   146  
   147  	if got := c.Stack().Stats().TCP.FailedConnectionAttempts.Value(); got != 1 {
   148  		t.Errorf("got stats.TCP.FailedConnectionAttempts.Value() = %d, want = 1", got)
   149  	}
   150  
   151  	if got := c.Stack().Stats().TCP.CurrentEstablished.Value(); got != 0 {
   152  		t.Errorf("got stats.TCP.CurrentEstablished.Value() = %d, want = 0", got)
   153  	}
   154  }
   155  
   156  // Test for ICMP error handling without completing handshake.
   157  func TestConnectICMPError(t *testing.T) {
   158  	c := context.New(t, e2e.DefaultMTU)
   159  	defer c.Cleanup()
   160  
   161  	var wq waiter.Queue
   162  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq)
   163  	if err != nil {
   164  		t.Fatalf("NewEndpoint failed: %s", err)
   165  	}
   166  
   167  	waitEntry, notifyCh := waiter.NewChannelEntry(waiter.EventHUp)
   168  	wq.EventRegister(&waitEntry)
   169  	defer wq.EventUnregister(&waitEntry)
   170  
   171  	{
   172  		err := ep.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort})
   173  		if d := cmp.Diff(&tcpip.ErrConnectStarted{}, err); d != "" {
   174  			t.Fatalf("ep.Connect(...) mismatch (-want +got):\n%s", d)
   175  		}
   176  	}
   177  
   178  	syn := c.GetPacket()
   179  	defer syn.Release()
   180  	checker.IPv4(t, syn, checker.TCP(checker.TCPFlags(header.TCPFlagSyn)))
   181  
   182  	wep := ep.(interface {
   183  		LastErrorLocked() tcpip.Error
   184  	})
   185  
   186  	c.SendICMPPacket(header.ICMPv4DstUnreachable, header.ICMPv4HostUnreachable, nil, syn, e2e.DefaultMTU)
   187  
   188  	for {
   189  		if err := wep.LastErrorLocked(); err != nil {
   190  			if d := cmp.Diff(&tcpip.ErrHostUnreachable{}, err); d != "" {
   191  				t.Errorf("ep.LastErrorLocked() mismatch (-want +got):\n%s", d)
   192  			}
   193  			break
   194  		}
   195  		time.Sleep(time.Millisecond)
   196  	}
   197  
   198  	<-notifyCh
   199  
   200  	// The stack would have unregistered the endpoint because of the ICMP error.
   201  	// Expect a RST for any subsequent packets sent to the endpoint.
   202  	c.SendPacket(nil, &context.Headers{
   203  		SrcPort: context.TestPort,
   204  		DstPort: context.StackPort,
   205  		Flags:   header.TCPFlagAck,
   206  		SeqNum:  seqnum.Value(context.TestInitialSequenceNumber) + 1,
   207  		AckNum:  c.IRS + 1,
   208  	})
   209  
   210  	b := c.GetPacket()
   211  	defer b.Release()
   212  	checker.IPv4(t, b, checker.TCP(
   213  		checker.SrcPort(context.StackPort),
   214  		checker.DstPort(context.TestPort),
   215  		checker.TCPSeqNum(uint32(c.IRS+1)),
   216  		checker.TCPAckNum(0),
   217  		checker.TCPFlags(header.TCPFlagRst)))
   218  }
   219  
   220  func TestConnectIncrementActiveConnection(t *testing.T) {
   221  	c := context.New(t, e2e.DefaultMTU)
   222  	defer c.Cleanup()
   223  
   224  	stats := c.Stack().Stats()
   225  	want := stats.TCP.ActiveConnectionOpenings.Value() + 1
   226  
   227  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
   228  	if got := stats.TCP.ActiveConnectionOpenings.Value(); got != want {
   229  		t.Errorf("got stats.TCP.ActtiveConnectionOpenings.Value() = %d, want = %d", got, want)
   230  	}
   231  }
   232  
   233  func TestConnectDoesNotIncrementFailedConnectionAttempts(t *testing.T) {
   234  	c := context.New(t, e2e.DefaultMTU)
   235  	defer c.Cleanup()
   236  
   237  	stats := c.Stack().Stats()
   238  	want := stats.TCP.FailedConnectionAttempts.Value()
   239  
   240  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
   241  	if got := stats.TCP.FailedConnectionAttempts.Value(); got != want {
   242  		t.Errorf("got stats.TCP.FailedConnectionAttempts.Value() = %d, want = %d", got, want)
   243  	}
   244  	if got := c.EP.Stats().(*tcp.Stats).FailedConnectionAttempts.Value(); got != want {
   245  		t.Errorf("got EP stats.FailedConnectionAttempts = %d, want = %d", got, want)
   246  	}
   247  }
   248  
   249  func TestActiveFailedConnectionAttemptIncrement(t *testing.T) {
   250  	c := context.New(t, e2e.DefaultMTU)
   251  	defer c.Cleanup()
   252  
   253  	stats := c.Stack().Stats()
   254  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
   255  	if err != nil {
   256  		t.Fatalf("NewEndpoint failed: %s", err)
   257  	}
   258  	c.EP = ep
   259  	want := stats.TCP.FailedConnectionAttempts.Value() + 1
   260  
   261  	{
   262  		err := c.EP.Connect(tcpip.FullAddress{NIC: 2, Addr: context.TestAddr, Port: context.TestPort})
   263  		if d := cmp.Diff(&tcpip.ErrHostUnreachable{}, err); d != "" {
   264  			t.Errorf("c.EP.Connect(...) mismatch (-want +got):\n%s", d)
   265  		}
   266  	}
   267  
   268  	if got := stats.TCP.FailedConnectionAttempts.Value(); got != want {
   269  		t.Errorf("got stats.TCP.FailedConnectionAttempts.Value() = %d, want = %d", got, want)
   270  	}
   271  	if got := c.EP.Stats().(*tcp.Stats).FailedConnectionAttempts.Value(); got != want {
   272  		t.Errorf("got EP stats FailedConnectionAttempts = %d, want = %d", got, want)
   273  	}
   274  }
   275  
   276  func TestCloseWithoutConnect(t *testing.T) {
   277  	c := context.New(t, e2e.DefaultMTU)
   278  	defer c.Cleanup()
   279  
   280  	// Create TCP endpoint.
   281  	var err tcpip.Error
   282  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
   283  	if err != nil {
   284  		t.Fatalf("NewEndpoint failed: %s", err)
   285  	}
   286  
   287  	c.EP.Close()
   288  	c.EP = nil
   289  
   290  	if got := c.Stack().Stats().TCP.CurrentConnected.Value(); got != 0 {
   291  		t.Errorf("got stats.TCP.CurrentConnected.Value() = %d, want = 0", got)
   292  	}
   293  }
   294  
   295  func TestHandshakeTimeoutConnectedCount(t *testing.T) {
   296  	clock := faketime.NewManualClock()
   297  	c := context.NewWithOpts(t, context.Options{
   298  		EnableV4: true,
   299  		EnableV6: true,
   300  		MTU:      e2e.DefaultMTU,
   301  		Clock:    clock,
   302  	})
   303  	defer c.Cleanup()
   304  
   305  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
   306  	if err != nil {
   307  		t.Fatalf("NewEndpoint failed: %s", err)
   308  	}
   309  	c.EP = ep
   310  
   311  	we, ch := waiter.NewChannelEntry(waiter.WritableEvents)
   312  	c.WQ.EventRegister(&we)
   313  	defer c.WQ.EventUnregister(&we)
   314  
   315  	switch err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}).(type) {
   316  	case *tcpip.ErrConnectStarted:
   317  	default:
   318  		t.Fatalf("Connect did not start: %v", err)
   319  	}
   320  
   321  	clock.Advance(tcp.DefaultKeepaliveInterval)
   322  	clock.Advance(tcp.DefaultKeepaliveInterval)
   323  	<-ch
   324  	switch err := c.EP.LastError().(type) {
   325  	case *tcpip.ErrTimeout:
   326  	default:
   327  		t.Fatalf("Connect didn't timeout: %v", err)
   328  	}
   329  	if got, want := c.Stack().Stats().TCP.CurrentConnected.Value(), uint64(0); got != want {
   330  		t.Fatalf("got stats.TCP.CurrentConnected.Value() = %d, want = %d", got, want)
   331  	}
   332  }
   333  
   334  func TestTCPSegmentsSentIncrement(t *testing.T) {
   335  	c := context.New(t, e2e.DefaultMTU)
   336  	defer c.Cleanup()
   337  
   338  	stats := c.Stack().Stats()
   339  	// SYN and ACK
   340  	want := stats.TCP.SegmentsSent.Value() + 2
   341  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
   342  
   343  	if got := stats.TCP.SegmentsSent.Value(); got != want {
   344  		t.Errorf("got stats.TCP.SegmentsSent.Value() = %d, want = %d", got, want)
   345  	}
   346  	if got := c.EP.Stats().(*tcp.Stats).SegmentsSent.Value(); got != want {
   347  		t.Errorf("got EP stats SegmentsSent.Value() = %d, want = %d", got, want)
   348  	}
   349  }
   350  
   351  func TestTCPResetsSentIncrement(t *testing.T) {
   352  	c := context.New(t, e2e.DefaultMTU)
   353  	defer c.Cleanup()
   354  	stats := c.Stack().Stats()
   355  	wq := &waiter.Queue{}
   356  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
   357  	if err != nil {
   358  		t.Fatalf("NewEndpoint failed: %s", err)
   359  	}
   360  	want := stats.TCP.SegmentsSent.Value() + 1
   361  
   362  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
   363  		t.Fatalf("Bind failed: %s", err)
   364  	}
   365  
   366  	if err := ep.Listen(10); err != nil {
   367  		t.Fatalf("Listen failed: %s", err)
   368  	}
   369  
   370  	// Send a SYN request.
   371  	iss := seqnum.Value(context.TestInitialSequenceNumber)
   372  	c.SendPacket(nil, &context.Headers{
   373  		SrcPort: context.TestPort,
   374  		DstPort: context.StackPort,
   375  		Flags:   header.TCPFlagSyn,
   376  		SeqNum:  iss,
   377  	})
   378  
   379  	// Receive the SYN-ACK reply.
   380  	v := c.GetPacket()
   381  	defer v.Release()
   382  	tcpHdr := header.TCP(header.IPv4(v.AsSlice()).Payload())
   383  	c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
   384  
   385  	ackHeaders := &context.Headers{
   386  		SrcPort: context.TestPort,
   387  		DstPort: context.StackPort,
   388  		Flags:   header.TCPFlagAck,
   389  		SeqNum:  iss + 1,
   390  		// If the AckNum is not the increment of the last sequence number, a RST
   391  		// segment is sent back in response.
   392  		AckNum: c.IRS + 2,
   393  	}
   394  
   395  	// Send ACK.
   396  	c.SendPacket(nil, ackHeaders)
   397  
   398  	v = c.GetPacket()
   399  	defer v.Release()
   400  
   401  	metricPollFn := func() error {
   402  		if got := stats.TCP.ResetsSent.Value(); got != want {
   403  			return fmt.Errorf("got stats.TCP.ResetsSent.Value() = %d, want = %d", got, want)
   404  		}
   405  		return nil
   406  	}
   407  	if err := testutil.Poll(metricPollFn, 1*time.Second); err != nil {
   408  		t.Error(err)
   409  	}
   410  }
   411  
   412  // TestTCPResetsSentNoICMP confirms that we don't get an ICMP DstUnreachable
   413  // packet when we try send a packet which is not part of an active session.
   414  func TestTCPResetsSentNoICMP(t *testing.T) {
   415  	c := context.New(t, e2e.DefaultMTU)
   416  	defer c.Cleanup()
   417  	stats := c.Stack().Stats()
   418  
   419  	// Send a SYN request for a closed port. This should elicit an RST
   420  	// but NOT an ICMPv4 DstUnreachable packet.
   421  	iss := seqnum.Value(context.TestInitialSequenceNumber)
   422  	c.SendPacket(nil, &context.Headers{
   423  		SrcPort: context.TestPort,
   424  		DstPort: context.StackPort,
   425  		Flags:   header.TCPFlagSyn,
   426  		SeqNum:  iss,
   427  	})
   428  
   429  	// Receive whatever comes back.
   430  	v := c.GetPacket()
   431  	defer v.Release()
   432  	ipHdr := header.IPv4(v.AsSlice())
   433  	if got, want := ipHdr.Protocol(), uint8(header.TCPProtocolNumber); got != want {
   434  		t.Errorf("unexpected protocol, got = %d, want = %d", got, want)
   435  	}
   436  
   437  	// Read outgoing ICMP stats and check no ICMP DstUnreachable was recorded.
   438  	sent := stats.ICMP.V4.PacketsSent
   439  	if got, want := sent.DstUnreachable.Value(), uint64(0); got != want {
   440  		t.Errorf("got ICMP DstUnreachable.Value() = %d, want = %d", got, want)
   441  	}
   442  }
   443  
   444  // TestTCPResetSentForACKWhenNotUsingSynCookies checks that the stack generates
   445  // a RST if an ACK is received on the listening socket for which there is no
   446  // active handshake in progress and we are not using SYN cookies.
   447  func TestTCPResetSentForACKWhenNotUsingSynCookies(t *testing.T) {
   448  	c := context.New(t, e2e.DefaultMTU)
   449  	defer c.Cleanup()
   450  
   451  	// Set TCPLingerTimeout to 5 seconds so that sockets are marked closed
   452  	wq := &waiter.Queue{}
   453  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
   454  	if err != nil {
   455  		t.Fatalf("NewEndpoint failed: %s", err)
   456  	}
   457  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
   458  		t.Fatalf("Bind failed: %s", err)
   459  	}
   460  
   461  	if err := ep.Listen(10); err != nil {
   462  		t.Fatalf("Listen failed: %s", err)
   463  	}
   464  
   465  	// Send a SYN request.
   466  	iss := seqnum.Value(context.TestInitialSequenceNumber)
   467  	c.SendPacket(nil, &context.Headers{
   468  		SrcPort: context.TestPort,
   469  		DstPort: context.StackPort,
   470  		Flags:   header.TCPFlagSyn,
   471  		SeqNum:  iss,
   472  	})
   473  
   474  	// Receive the SYN-ACK reply.
   475  	v := c.GetPacket()
   476  	defer v.Release()
   477  	tcpHdr := header.TCP(header.IPv4(v.AsSlice()).Payload())
   478  	c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
   479  
   480  	ackHeaders := &context.Headers{
   481  		SrcPort: context.TestPort,
   482  		DstPort: context.StackPort,
   483  		Flags:   header.TCPFlagAck,
   484  		SeqNum:  iss + 1,
   485  		AckNum:  c.IRS + 1,
   486  	}
   487  
   488  	// Send ACK.
   489  	c.SendPacket(nil, ackHeaders)
   490  
   491  	// Try to accept the connection.
   492  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
   493  	wq.EventRegister(&we)
   494  	defer wq.EventUnregister(&we)
   495  
   496  	c.EP, _, err = ep.Accept(nil)
   497  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
   498  		// Wait for connection to be established.
   499  		select {
   500  		case <-ch:
   501  			c.EP, _, err = ep.Accept(nil)
   502  			if err != nil {
   503  				t.Fatalf("Accept failed: %s", err)
   504  			}
   505  
   506  		case <-time.After(1 * time.Second):
   507  			t.Fatalf("Timed out waiting for accept")
   508  		}
   509  	}
   510  
   511  	// Lower stackwide TIME_WAIT timeout so that the reservations
   512  	// are released instantly on Close.
   513  	tcpTW := tcpip.TCPTimeWaitTimeoutOption(1 * time.Millisecond)
   514  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &tcpTW); err != nil {
   515  		t.Fatalf("SetTransportProtocolOption(%d, &%T(%d)): %s", tcp.ProtocolNumber, tcpTW, tcpTW, err)
   516  	}
   517  
   518  	c.EP.Close()
   519  	b := c.GetPacket()
   520  	defer b.Release()
   521  	checker.IPv4(t, b, checker.TCP(
   522  		checker.SrcPort(context.StackPort),
   523  		checker.DstPort(context.TestPort),
   524  		checker.TCPSeqNum(uint32(c.IRS+1)),
   525  		checker.TCPAckNum(uint32(iss)+1),
   526  		checker.TCPFlags(header.TCPFlagFin|header.TCPFlagAck)))
   527  	finHeaders := &context.Headers{
   528  		SrcPort: context.TestPort,
   529  		DstPort: context.StackPort,
   530  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
   531  		SeqNum:  iss + 1,
   532  		AckNum:  c.IRS + 2,
   533  	}
   534  
   535  	c.SendPacket(nil, finHeaders)
   536  
   537  	// Get the ACK to the FIN we just sent.
   538  	b = c.GetPacket()
   539  	defer b.Release()
   540  
   541  	// Since an active close was done we need to wait for a little more than
   542  	// tcpLingerTimeout for the port reservations to be released and the
   543  	// socket to move to a CLOSED state.
   544  	time.Sleep(20 * time.Millisecond)
   545  
   546  	// Now resend the same ACK, this ACK should generate a RST as there
   547  	// should be no endpoint in SYN-RCVD state and we are not using
   548  	// syn-cookies yet. The reason we send the same ACK is we need a valid
   549  	// cookie(IRS) generated by the netstack without which the ACK will be
   550  	// rejected.
   551  	c.SendPacket(nil, ackHeaders)
   552  
   553  	b = c.GetPacket()
   554  	defer b.Release()
   555  	checker.IPv4(t, b, checker.TCP(
   556  		checker.SrcPort(context.StackPort),
   557  		checker.DstPort(context.TestPort),
   558  		checker.TCPSeqNum(uint32(c.IRS+1)),
   559  		checker.TCPAckNum(0),
   560  		checker.TCPFlags(header.TCPFlagRst)))
   561  }
   562  
   563  func TestTCPResetsReceivedIncrement(t *testing.T) {
   564  	c := context.New(t, e2e.DefaultMTU)
   565  	defer c.Cleanup()
   566  
   567  	stats := c.Stack().Stats()
   568  	want := stats.TCP.ResetsReceived.Value() + 1
   569  	iss := seqnum.Value(context.TestInitialSequenceNumber)
   570  	rcvWnd := seqnum.Size(30000)
   571  	c.CreateConnected(iss, rcvWnd, -1 /* epRcvBuf */)
   572  
   573  	c.SendPacket(nil, &context.Headers{
   574  		SrcPort: context.TestPort,
   575  		DstPort: c.Port,
   576  		SeqNum:  iss.Add(1),
   577  		AckNum:  c.IRS.Add(1),
   578  		RcvWnd:  rcvWnd,
   579  		Flags:   header.TCPFlagRst,
   580  	})
   581  
   582  	if got := stats.TCP.ResetsReceived.Value(); got != want {
   583  		t.Errorf("got stats.TCP.ResetsReceived.Value() = %d, want = %d", got, want)
   584  	}
   585  }
   586  
   587  func TestTCPResetsDoNotGenerateResets(t *testing.T) {
   588  	c := context.New(t, e2e.DefaultMTU)
   589  	defer c.Cleanup()
   590  
   591  	stats := c.Stack().Stats()
   592  	want := stats.TCP.ResetsReceived.Value() + 1
   593  	iss := seqnum.Value(context.TestInitialSequenceNumber)
   594  	rcvWnd := seqnum.Size(30000)
   595  	c.CreateConnected(iss, rcvWnd, -1 /* epRcvBuf */)
   596  
   597  	c.SendPacket(nil, &context.Headers{
   598  		SrcPort: context.TestPort,
   599  		DstPort: c.Port,
   600  		SeqNum:  iss.Add(1),
   601  		AckNum:  c.IRS.Add(1),
   602  		RcvWnd:  rcvWnd,
   603  		Flags:   header.TCPFlagRst,
   604  	})
   605  
   606  	if got := stats.TCP.ResetsReceived.Value(); got != want {
   607  		t.Errorf("got stats.TCP.ResetsReceived.Value() = %d, want = %d", got, want)
   608  	}
   609  	c.CheckNoPacketTimeout("got an unexpected packet", 100*time.Millisecond)
   610  }
   611  
   612  func TestActiveHandshake(t *testing.T) {
   613  	c := context.New(t, e2e.DefaultMTU)
   614  	defer c.Cleanup()
   615  
   616  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
   617  }
   618  
   619  func TestNonBlockingClose(t *testing.T) {
   620  	c := context.New(t, e2e.DefaultMTU)
   621  	defer c.Cleanup()
   622  
   623  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
   624  	ep := c.EP
   625  	c.EP = nil
   626  
   627  	// Close the endpoint and measure how long it takes.
   628  	t0 := time.Now()
   629  	ep.Close()
   630  	if diff := time.Now().Sub(t0); diff > 3*time.Second {
   631  		t.Fatalf("Took too long to close: %s", diff)
   632  	}
   633  }
   634  
   635  func TestConnectResetAfterClose(t *testing.T) {
   636  	c := context.New(t, e2e.DefaultMTU)
   637  	defer c.Cleanup()
   638  
   639  	// Set TCPLinger to 3 seconds so that sockets are marked closed
   640  	// after 3 second in FIN_WAIT2 state.
   641  	tcpLingerTimeout := 3 * time.Second
   642  	opt := tcpip.TCPLingerTimeoutOption(tcpLingerTimeout)
   643  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
   644  		t.Fatalf("SetTransportProtocolOption(%d, &%T(%d)): %s", tcp.ProtocolNumber, opt, opt, err)
   645  	}
   646  
   647  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
   648  	ep := c.EP
   649  	c.EP = nil
   650  
   651  	// Close the endpoint, make sure we get a FIN segment, then acknowledge
   652  	// to complete closure of sender, but don't send our own FIN.
   653  	ep.Close()
   654  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
   655  	b := c.GetPacket()
   656  	defer b.Release()
   657  	checker.IPv4(t, b,
   658  		checker.TCP(
   659  			checker.DstPort(context.TestPort),
   660  			checker.TCPSeqNum(uint32(c.IRS)+1),
   661  			checker.TCPAckNum(uint32(iss)),
   662  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
   663  		),
   664  	)
   665  	c.SendPacket(nil, &context.Headers{
   666  		SrcPort: context.TestPort,
   667  		DstPort: c.Port,
   668  		Flags:   header.TCPFlagAck,
   669  		SeqNum:  iss,
   670  		AckNum:  c.IRS.Add(2),
   671  		RcvWnd:  30000,
   672  	})
   673  
   674  	// Wait for the ep to give up waiting for a FIN.
   675  	time.Sleep(tcpLingerTimeout + 1*time.Second)
   676  
   677  	// Now send an ACK and it should trigger a RST as the endpoint should
   678  	// not exist anymore.
   679  	c.SendPacket(nil, &context.Headers{
   680  		SrcPort: context.TestPort,
   681  		DstPort: c.Port,
   682  		Flags:   header.TCPFlagAck,
   683  		SeqNum:  iss,
   684  		AckNum:  c.IRS.Add(2),
   685  		RcvWnd:  30000,
   686  	})
   687  
   688  	for {
   689  		v := c.GetPacket()
   690  		defer v.Release()
   691  		tcpHdr := header.TCP(header.IPv4(v.AsSlice()).Payload())
   692  		if tcpHdr.Flags() == header.TCPFlagAck|header.TCPFlagFin {
   693  			// This is a retransmit of the FIN, ignore it.
   694  			continue
   695  		}
   696  
   697  		checker.IPv4(t, v,
   698  			checker.TCP(
   699  				checker.DstPort(context.TestPort),
   700  				// RST is always generated with sndNxt which if the FIN
   701  				// has been sent will be 1 higher than the sequence number
   702  				// of the FIN itself.
   703  				checker.TCPSeqNum(uint32(c.IRS)+2),
   704  				checker.TCPAckNum(0),
   705  				checker.TCPFlags(header.TCPFlagRst),
   706  			),
   707  		)
   708  		break
   709  	}
   710  }
   711  
   712  // TestCurrentConnectedIncrement tests increment of the current
   713  // established and connected counters.
   714  func TestCurrentConnectedIncrement(t *testing.T) {
   715  	c := context.New(t, e2e.DefaultMTU)
   716  	defer c.Cleanup()
   717  
   718  	// Set TCPTimeWaitTimeout to 1 seconds so that sockets are marked closed
   719  	// after 1 second in TIME_WAIT state.
   720  	tcpTimeWaitTimeout := 1 * time.Second
   721  	opt := tcpip.TCPTimeWaitTimeoutOption(tcpTimeWaitTimeout)
   722  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
   723  		t.Fatalf("SetTransportProtocolOption(%d, &%T(%d)): %s", tcp.ProtocolNumber, opt, opt, err)
   724  	}
   725  
   726  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
   727  	ep := c.EP
   728  	c.EP = nil
   729  
   730  	if got := c.Stack().Stats().TCP.CurrentEstablished.Value(); got != 1 {
   731  		t.Errorf("got stats.TCP.CurrentEstablished.Value() = %d, want = 1", got)
   732  	}
   733  	gotConnected := c.Stack().Stats().TCP.CurrentConnected.Value()
   734  	if gotConnected != 1 {
   735  		t.Errorf("got stats.TCP.CurrentConnected.Value() = %d, want = 1", gotConnected)
   736  	}
   737  
   738  	ep.Close()
   739  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
   740  	b := c.GetPacket()
   741  	defer b.Release()
   742  	checker.IPv4(t, b,
   743  		checker.TCP(
   744  			checker.DstPort(context.TestPort),
   745  			checker.TCPSeqNum(uint32(c.IRS)+1),
   746  			checker.TCPAckNum(uint32(iss)),
   747  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
   748  		),
   749  	)
   750  	c.SendPacket(nil, &context.Headers{
   751  		SrcPort: context.TestPort,
   752  		DstPort: c.Port,
   753  		Flags:   header.TCPFlagAck,
   754  		SeqNum:  iss,
   755  		AckNum:  c.IRS.Add(2),
   756  		RcvWnd:  30000,
   757  	})
   758  
   759  	if got := c.Stack().Stats().TCP.CurrentEstablished.Value(); got != 0 {
   760  		t.Errorf("got stats.TCP.CurrentEstablished.Value() = %d, want = 0", got)
   761  	}
   762  	if got := c.Stack().Stats().TCP.CurrentConnected.Value(); got != gotConnected {
   763  		t.Errorf("got stats.TCP.CurrentConnected.Value() = %d, want = %d", got, gotConnected)
   764  	}
   765  
   766  	// Ack and send FIN as well.
   767  	c.SendPacket(nil, &context.Headers{
   768  		SrcPort: context.TestPort,
   769  		DstPort: c.Port,
   770  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
   771  		SeqNum:  iss,
   772  		AckNum:  c.IRS.Add(2),
   773  		RcvWnd:  30000,
   774  	})
   775  
   776  	// Check that the stack acks the FIN.
   777  	v := c.GetPacket()
   778  	defer v.Release()
   779  	checker.IPv4(t, v,
   780  		checker.PayloadLen(header.TCPMinimumSize),
   781  		checker.TCP(
   782  			checker.DstPort(context.TestPort),
   783  			checker.TCPSeqNum(uint32(c.IRS)+2),
   784  			checker.TCPAckNum(uint32(iss)+1),
   785  			checker.TCPFlags(header.TCPFlagAck),
   786  		),
   787  	)
   788  
   789  	// Wait for a little more than the TIME-WAIT duration for the socket to
   790  	// transition to CLOSED state.
   791  	time.Sleep(1200 * time.Millisecond)
   792  
   793  	if got := c.Stack().Stats().TCP.CurrentEstablished.Value(); got != 0 {
   794  		t.Errorf("got stats.TCP.CurrentEstablished.Value() = %d, want = 0", got)
   795  	}
   796  	if got := c.Stack().Stats().TCP.CurrentConnected.Value(); got != 0 {
   797  		t.Errorf("got stats.TCP.CurrentConnected.Value() = %d, want = 0", got)
   798  	}
   799  }
   800  
   801  // TestClosingWithEnqueuedSegments tests handling of still enqueued segments
   802  // when the endpoint transitions to StateClose. The in-flight segments would be
   803  // re-enqueued to a any listening endpoint.
   804  func TestClosingWithEnqueuedSegments(t *testing.T) {
   805  	c := context.New(t, e2e.DefaultMTU)
   806  	defer c.Cleanup()
   807  
   808  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
   809  	ep := c.EP
   810  	c.EP = nil
   811  
   812  	if got, want := tcp.EndpointState(ep.State()), tcp.StateEstablished; got != want {
   813  		t.Errorf("unexpected endpoint state: want %d, got %d", want, got)
   814  	}
   815  
   816  	// Send a FIN for ESTABLISHED --> CLOSED-WAIT
   817  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
   818  	c.SendPacket(nil, &context.Headers{
   819  		SrcPort: context.TestPort,
   820  		DstPort: c.Port,
   821  		Flags:   header.TCPFlagFin | header.TCPFlagAck,
   822  		SeqNum:  iss,
   823  		AckNum:  c.IRS.Add(1),
   824  		RcvWnd:  30000,
   825  	})
   826  
   827  	// Get the ACK for the FIN we sent.
   828  	v := c.GetPacket()
   829  	defer v.Release()
   830  	checker.IPv4(t, v,
   831  		checker.TCP(
   832  			checker.DstPort(context.TestPort),
   833  			checker.TCPSeqNum(uint32(c.IRS)+1),
   834  			checker.TCPAckNum(uint32(iss)+1),
   835  			checker.TCPFlags(header.TCPFlagAck),
   836  		),
   837  	)
   838  
   839  	// Give the stack a few ms to transition the endpoint out of ESTABLISHED
   840  	// state.
   841  	time.Sleep(10 * time.Millisecond)
   842  
   843  	if got, want := tcp.EndpointState(ep.State()), tcp.StateCloseWait; got != want {
   844  		t.Errorf("unexpected endpoint state: want %d, got %d", want, got)
   845  	}
   846  
   847  	// Close the application endpoint for CLOSE_WAIT --> LAST_ACK
   848  	ep.Close()
   849  
   850  	// Get the FIN
   851  	v = c.GetPacket()
   852  	defer v.Release()
   853  	checker.IPv4(t, v,
   854  		checker.TCP(
   855  			checker.DstPort(context.TestPort),
   856  			checker.TCPSeqNum(uint32(c.IRS)+1),
   857  			checker.TCPAckNum(uint32(iss)+1),
   858  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
   859  		),
   860  	)
   861  
   862  	if got, want := tcp.EndpointState(ep.State()), tcp.StateLastAck; got != want {
   863  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
   864  	}
   865  
   866  	// Pause the endpoint`s protocolMainLoop.
   867  	ep.(interface{ StopWork() }).StopWork()
   868  
   869  	// Enqueue last ACK followed by an ACK matching the endpoint
   870  	//
   871  	// Send Last ACK for LAST_ACK --> CLOSED
   872  	c.SendPacket(nil, &context.Headers{
   873  		SrcPort: context.TestPort,
   874  		DstPort: c.Port,
   875  		Flags:   header.TCPFlagAck,
   876  		SeqNum:  iss.Add(1),
   877  		AckNum:  c.IRS.Add(2),
   878  		RcvWnd:  30000,
   879  	})
   880  
   881  	// Send a packet with ACK set, this would generate RST when
   882  	// not using SYN cookies as in this test.
   883  	c.SendPacket(nil, &context.Headers{
   884  		SrcPort: context.TestPort,
   885  		DstPort: c.Port,
   886  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
   887  		SeqNum:  iss.Add(2),
   888  		AckNum:  c.IRS.Add(2),
   889  		RcvWnd:  30000,
   890  	})
   891  
   892  	// Unpause endpoint`s protocolMainLoop.
   893  	ep.(interface{ ResumeWork() }).ResumeWork()
   894  
   895  	// Wait for the protocolMainLoop to resume and update state.
   896  	time.Sleep(10 * time.Millisecond)
   897  
   898  	// Expect the endpoint to be closed.
   899  	if got, want := tcp.EndpointState(ep.State()), tcp.StateClose; got != want {
   900  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
   901  	}
   902  
   903  	if got := c.Stack().Stats().TCP.EstablishedClosed.Value(); got != 1 {
   904  		t.Errorf("got c.Stack().Stats().TCP.EstablishedClosed = %d, want = 1", got)
   905  	}
   906  
   907  	if got := c.Stack().Stats().TCP.CurrentEstablished.Value(); got != 0 {
   908  		t.Errorf("got stats.TCP.CurrentEstablished.Value() = %d, want = 0", got)
   909  	}
   910  
   911  	// Check if the endpoint was moved to CLOSED and netstack sent a reset in
   912  	// response to the ACK packet that we sent after last-ACK.
   913  	v = c.GetPacket()
   914  	defer v.Release()
   915  	checker.IPv4(t, v,
   916  		checker.TCP(
   917  			checker.DstPort(context.TestPort),
   918  			checker.TCPSeqNum(uint32(c.IRS)+2),
   919  			checker.TCPAckNum(0),
   920  			checker.TCPFlags(header.TCPFlagRst),
   921  		),
   922  	)
   923  }
   924  
   925  func TestSimpleReceive(t *testing.T) {
   926  	c := context.New(t, e2e.DefaultMTU)
   927  	defer c.Cleanup()
   928  
   929  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
   930  
   931  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
   932  	c.WQ.EventRegister(&we)
   933  	defer c.WQ.EventUnregister(&we)
   934  
   935  	ept := endpointTester{c.EP}
   936  
   937  	data := []byte{1, 2, 3}
   938  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
   939  	c.SendPacket(data, &context.Headers{
   940  		SrcPort: context.TestPort,
   941  		DstPort: c.Port,
   942  		Flags:   header.TCPFlagAck,
   943  		SeqNum:  iss,
   944  		AckNum:  c.IRS.Add(1),
   945  		RcvWnd:  30000,
   946  	})
   947  
   948  	// Wait for receive to be notified.
   949  	select {
   950  	case <-ch:
   951  	case <-time.After(1 * time.Second):
   952  		t.Fatalf("Timed out waiting for data to arrive")
   953  	}
   954  
   955  	// Receive data.
   956  	v := ept.CheckRead(t)
   957  	if !bytes.Equal(data, v) {
   958  		t.Fatalf("got data = %v, want = %v", v, data)
   959  	}
   960  
   961  	// Check that ACK is received.
   962  	b := c.GetPacket()
   963  	defer b.Release()
   964  	checker.IPv4(t, b,
   965  		checker.TCP(
   966  			checker.DstPort(context.TestPort),
   967  			checker.TCPSeqNum(uint32(c.IRS)+1),
   968  			checker.TCPAckNum(uint32(iss)+uint32(len(data))),
   969  			checker.TCPFlags(header.TCPFlagAck),
   970  		),
   971  	)
   972  }
   973  
   974  // TestUserSuppliedMSSOnConnect tests that the user supplied MSS is used when
   975  // creating a new active TCP socket. It should be present in the sent TCP
   976  // SYN segment.
   977  func TestUserSuppliedMSSOnConnect(t *testing.T) {
   978  	const mtu = 5000
   979  
   980  	ips := []struct {
   981  		name        string
   982  		createEP    func(*context.Context)
   983  		connectAddr tcpip.Address
   984  		checker     func(*testing.T, *context.Context, uint16, int)
   985  		maxMSS      uint16
   986  	}{
   987  		{
   988  			name: "IPv4",
   989  			createEP: func(c *context.Context) {
   990  				c.Create(-1)
   991  			},
   992  			connectAddr: context.TestAddr,
   993  			checker: func(t *testing.T, c *context.Context, mss uint16, ws int) {
   994  				v := c.GetPacket()
   995  				defer v.Release()
   996  				checker.IPv4(t, v, checker.TCP(
   997  					checker.DstPort(context.TestPort),
   998  					checker.TCPFlags(header.TCPFlagSyn),
   999  					checker.TCPSynOptions(header.TCPSynOptions{MSS: mss, WS: ws})))
  1000  			},
  1001  			maxMSS: mtu - header.IPv4MinimumSize - header.TCPMinimumSize,
  1002  		},
  1003  		{
  1004  			name: "IPv6",
  1005  			createEP: func(c *context.Context) {
  1006  				c.CreateV6Endpoint(true)
  1007  			},
  1008  			connectAddr: context.TestV6Addr,
  1009  			checker: func(t *testing.T, c *context.Context, mss uint16, ws int) {
  1010  				v := c.GetV6Packet()
  1011  				defer v.Release()
  1012  				checker.IPv6(t, v, checker.TCP(
  1013  					checker.DstPort(context.TestPort),
  1014  					checker.TCPFlags(header.TCPFlagSyn),
  1015  					checker.TCPSynOptions(header.TCPSynOptions{MSS: mss, WS: ws})))
  1016  			},
  1017  			maxMSS: mtu - header.IPv6MinimumSize - header.TCPMinimumSize,
  1018  		},
  1019  	}
  1020  
  1021  	for _, ip := range ips {
  1022  		t.Run(ip.name, func(t *testing.T) {
  1023  			tests := []struct {
  1024  				name   string
  1025  				setMSS uint16
  1026  				expMSS uint16
  1027  			}{
  1028  				{
  1029  					name:   "EqualToMaxMSS",
  1030  					setMSS: ip.maxMSS,
  1031  					expMSS: ip.maxMSS,
  1032  				},
  1033  				{
  1034  					name:   "LessThanMaxMSS",
  1035  					setMSS: ip.maxMSS - 1,
  1036  					expMSS: ip.maxMSS - 1,
  1037  				},
  1038  				{
  1039  					name:   "GreaterThanMaxMSS",
  1040  					setMSS: ip.maxMSS + 1,
  1041  					expMSS: ip.maxMSS,
  1042  				},
  1043  			}
  1044  
  1045  			for _, test := range tests {
  1046  				t.Run(test.name, func(t *testing.T) {
  1047  					c := context.New(t, mtu)
  1048  					defer c.Cleanup()
  1049  
  1050  					ip.createEP(c)
  1051  
  1052  					// Set the MSS socket option.
  1053  					if err := c.EP.SetSockOptInt(tcpip.MaxSegOption, int(test.setMSS)); err != nil {
  1054  						t.Fatalf("SetSockOptInt(MaxSegOption, %d): %s", test.setMSS, err)
  1055  					}
  1056  
  1057  					// Get expected window size.
  1058  					rcvBufSize := c.EP.SocketOptions().GetReceiveBufferSize()
  1059  					ws := tcp.FindWndScale(seqnum.Size(rcvBufSize))
  1060  
  1061  					connectAddr := tcpip.FullAddress{Addr: ip.connectAddr, Port: context.TestPort}
  1062  					{
  1063  						err := c.EP.Connect(connectAddr)
  1064  						if d := cmp.Diff(&tcpip.ErrConnectStarted{}, err); d != "" {
  1065  							t.Fatalf("Connect(%+v) mismatch (-want +got):\n%s", connectAddr, d)
  1066  						}
  1067  					}
  1068  
  1069  					// Receive SYN packet with our user supplied MSS.
  1070  					ip.checker(t, c, test.expMSS, ws)
  1071  				})
  1072  			}
  1073  		})
  1074  	}
  1075  }
  1076  
  1077  // TestUserSuppliedMSSOnListenAccept tests that the user supplied MSS is used
  1078  // when completing the handshake for a new TCP connection from a TCP
  1079  // listening socket. It should be present in the sent TCP SYN-ACK segment.
  1080  func TestUserSuppliedMSSOnListenAccept(t *testing.T) {
  1081  	const mtu = 5000
  1082  
  1083  	ips := []struct {
  1084  		name     string
  1085  		createEP func(*context.Context)
  1086  		sendPkt  func(*context.Context, *context.Headers)
  1087  		checker  func(*testing.T, *context.Context, uint16, uint16)
  1088  		maxMSS   uint16
  1089  	}{
  1090  		{
  1091  			name: "IPv4",
  1092  			createEP: func(c *context.Context) {
  1093  				c.Create(-1)
  1094  			},
  1095  			sendPkt: func(c *context.Context, h *context.Headers) {
  1096  				c.SendPacket(nil, h)
  1097  			},
  1098  			checker: func(t *testing.T, c *context.Context, srcPort, mss uint16) {
  1099  				v := c.GetPacket()
  1100  				defer v.Release()
  1101  				checker.IPv4(t, v, checker.TCP(
  1102  					checker.DstPort(srcPort),
  1103  					checker.TCPFlags(header.TCPFlagSyn|header.TCPFlagAck),
  1104  					checker.TCPSynOptions(header.TCPSynOptions{MSS: mss, WS: -1})))
  1105  			},
  1106  			maxMSS: mtu - header.IPv4MinimumSize - header.TCPMinimumSize,
  1107  		},
  1108  		{
  1109  			name: "IPv6",
  1110  			createEP: func(c *context.Context) {
  1111  				c.CreateV6Endpoint(false)
  1112  			},
  1113  			sendPkt: func(c *context.Context, h *context.Headers) {
  1114  				c.SendV6Packet(nil, h)
  1115  			},
  1116  			checker: func(t *testing.T, c *context.Context, srcPort, mss uint16) {
  1117  				v := c.GetV6Packet()
  1118  				defer v.Release()
  1119  				checker.IPv6(t, v, checker.TCP(
  1120  					checker.DstPort(srcPort),
  1121  					checker.TCPFlags(header.TCPFlagSyn|header.TCPFlagAck),
  1122  					checker.TCPSynOptions(header.TCPSynOptions{MSS: mss, WS: -1})))
  1123  			},
  1124  			maxMSS: mtu - header.IPv6MinimumSize - header.TCPMinimumSize,
  1125  		},
  1126  	}
  1127  
  1128  	for _, ip := range ips {
  1129  		t.Run(ip.name, func(t *testing.T) {
  1130  			tests := []struct {
  1131  				name   string
  1132  				setMSS uint16
  1133  				expMSS uint16
  1134  			}{
  1135  				{
  1136  					name:   "EqualToMaxMSS",
  1137  					setMSS: ip.maxMSS,
  1138  					expMSS: ip.maxMSS,
  1139  				},
  1140  				{
  1141  					name:   "LessThanMaxMSS",
  1142  					setMSS: ip.maxMSS - 1,
  1143  					expMSS: ip.maxMSS - 1,
  1144  				},
  1145  				{
  1146  					name:   "GreaterThanMaxMSS",
  1147  					setMSS: ip.maxMSS + 1,
  1148  					expMSS: ip.maxMSS,
  1149  				},
  1150  			}
  1151  
  1152  			for _, test := range tests {
  1153  				t.Run(test.name, func(t *testing.T) {
  1154  					c := context.New(t, mtu)
  1155  					defer c.Cleanup()
  1156  
  1157  					ip.createEP(c)
  1158  
  1159  					if err := c.EP.SetSockOptInt(tcpip.MaxSegOption, int(test.setMSS)); err != nil {
  1160  						t.Fatalf("SetSockOptInt(MaxSegOption, %d): %s", test.setMSS, err)
  1161  					}
  1162  
  1163  					bindAddr := tcpip.FullAddress{Port: context.StackPort}
  1164  					if err := c.EP.Bind(bindAddr); err != nil {
  1165  						t.Fatalf("Bind(%+v): %s:", bindAddr, err)
  1166  					}
  1167  
  1168  					backlog := 5
  1169  					// Keep the number of client requests twice to the backlog
  1170  					// such that half of the connections do not use syncookies
  1171  					// and the other half does.
  1172  					clientConnects := backlog * 2
  1173  
  1174  					if err := c.EP.Listen(backlog); err != nil {
  1175  						t.Fatalf("Listen(%d): %s:", backlog, err)
  1176  					}
  1177  
  1178  					for i := 0; i < clientConnects; i++ {
  1179  						// Send a SYN requests.
  1180  						iss := seqnum.Value(i)
  1181  						srcPort := context.TestPort + uint16(i)
  1182  						ip.sendPkt(c, &context.Headers{
  1183  							SrcPort: srcPort,
  1184  							DstPort: context.StackPort,
  1185  							Flags:   header.TCPFlagSyn,
  1186  							SeqNum:  iss,
  1187  						})
  1188  
  1189  						// Receive the SYN-ACK reply.
  1190  						ip.checker(t, c, srcPort, test.expMSS)
  1191  					}
  1192  				})
  1193  			}
  1194  		})
  1195  	}
  1196  }
  1197  func TestSendRstOnListenerRxSynAckV4(t *testing.T) {
  1198  	c := context.New(t, e2e.DefaultMTU)
  1199  	defer c.Cleanup()
  1200  
  1201  	c.Create(-1)
  1202  
  1203  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  1204  		t.Fatal("Bind failed:", err)
  1205  	}
  1206  
  1207  	if err := c.EP.Listen(10); err != nil {
  1208  		t.Fatal("Listen failed:", err)
  1209  	}
  1210  
  1211  	c.SendPacket(nil, &context.Headers{
  1212  		SrcPort: context.TestPort,
  1213  		DstPort: context.StackPort,
  1214  		Flags:   header.TCPFlagSyn | header.TCPFlagAck,
  1215  		SeqNum:  100,
  1216  		AckNum:  200,
  1217  	})
  1218  
  1219  	v := c.GetPacket()
  1220  	defer v.Release()
  1221  	checker.IPv4(t, v, checker.TCP(
  1222  		checker.DstPort(context.TestPort),
  1223  		checker.TCPFlags(header.TCPFlagRst),
  1224  		checker.TCPSeqNum(200)))
  1225  }
  1226  
  1227  func TestSendRstOnListenerRxSynAckV6(t *testing.T) {
  1228  	c := context.New(t, e2e.DefaultMTU)
  1229  	defer c.Cleanup()
  1230  
  1231  	c.CreateV6Endpoint(true)
  1232  
  1233  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  1234  		t.Fatal("Bind failed:", err)
  1235  	}
  1236  
  1237  	if err := c.EP.Listen(10); err != nil {
  1238  		t.Fatal("Listen failed:", err)
  1239  	}
  1240  
  1241  	c.SendV6Packet(nil, &context.Headers{
  1242  		SrcPort: context.TestPort,
  1243  		DstPort: context.StackPort,
  1244  		Flags:   header.TCPFlagSyn | header.TCPFlagAck,
  1245  		SeqNum:  100,
  1246  		AckNum:  200,
  1247  	})
  1248  
  1249  	v := c.GetV6Packet()
  1250  	defer v.Release()
  1251  	checker.IPv6(t, v, checker.TCP(
  1252  		checker.DstPort(context.TestPort),
  1253  		checker.TCPFlags(header.TCPFlagRst),
  1254  		checker.TCPSeqNum(200)))
  1255  }
  1256  
  1257  // TestNoSynCookieWithoutOverflow tests that SYN-COOKIEs are not issued when the
  1258  // queue is not overflowing. That is as long as newly completed connections are being
  1259  // accepted we do not see a SYN-COOKIE even > 2x listen backlog number of connections
  1260  // are accepted.
  1261  func TestNoSynCookieWithoutOverflow(t *testing.T) {
  1262  	c := context.New(t, e2e.DefaultMTU)
  1263  	defer c.Cleanup()
  1264  
  1265  	c.Create(-1)
  1266  
  1267  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  1268  		t.Fatal("Bind failed:", err)
  1269  	}
  1270  
  1271  	const backlog = 10
  1272  	if err := c.EP.Listen(backlog); err != nil {
  1273  		t.Fatal("Listen failed:", err)
  1274  	}
  1275  
  1276  	doOne := func(portIndex int) {
  1277  		// Try to accept the connection.
  1278  		we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  1279  		c.WQ.EventRegister(&we)
  1280  		defer c.WQ.EventUnregister(&we)
  1281  
  1282  		_, _ = executeHandshake(t, c, context.TestPort+uint16(portIndex), false /* synCookiesInUse */)
  1283  
  1284  		_, _, err := c.EP.Accept(nil)
  1285  		if err == nil {
  1286  			return
  1287  		}
  1288  		switch {
  1289  		case cmp.Equal(&tcpip.ErrWouldBlock{}, err):
  1290  			{
  1291  				select {
  1292  				case <-ch:
  1293  					_, _, err = c.EP.Accept(nil)
  1294  					if err != nil {
  1295  						t.Fatalf("Accept failed: %s", err)
  1296  					}
  1297  				case <-time.After(1 * time.Second):
  1298  					t.Fatalf("Timed out waiting for accept")
  1299  				}
  1300  			}
  1301  		default:
  1302  			t.Fatalf("Accept failed: %s", err)
  1303  		}
  1304  	}
  1305  
  1306  	for i := 0; i < backlog*5; i++ {
  1307  		doOne(i)
  1308  	}
  1309  }
  1310  
  1311  // TestNoSynCookieOnFailedHandshakes tests that failed handshakes clear
  1312  // endpoints from the pending queue. This is tested by verifying that the
  1313  // SYN-ACK from the stack carries a valid window scale despite > 2xbacklog
  1314  // handshakes failing.
  1315  //
  1316  // If a failed handshake was not resulting in removal from pending endpoints
  1317  // list for the accepting endpoint then it will eventually result in a
  1318  // SYN-COOKIE which we can identify with a SYN-ACK w/ a WS of -1.
  1319  func TestNoSynCookieOnFailedHandshakes(t *testing.T) {
  1320  	c := context.New(t, e2e.DefaultMTU)
  1321  	defer c.Cleanup()
  1322  
  1323  	c.Create(-1)
  1324  
  1325  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  1326  		t.Fatal("Bind failed:", err)
  1327  	}
  1328  
  1329  	const backlog = 10
  1330  	if err := c.EP.Listen(backlog); err != nil {
  1331  		t.Fatal("Listen failed:", err)
  1332  	}
  1333  
  1334  	doOne := func() {
  1335  		// Send a SYN request.
  1336  		options := []byte{header.TCPOptionWS, 3, 0, header.TCPOptionNOP}
  1337  		irs := seqnum.Value(context.TestInitialSequenceNumber)
  1338  		c.SendPacket(nil, &context.Headers{
  1339  			SrcPort: context.TestPort,
  1340  			DstPort: context.StackPort,
  1341  			Flags:   header.TCPFlagSyn,
  1342  			SeqNum:  irs,
  1343  			RcvWnd:  30000,
  1344  			TCPOpts: options,
  1345  		})
  1346  
  1347  		// Receive the SYN-ACK reply.
  1348  		v := c.GetPacket()
  1349  		defer v.Release()
  1350  		tcpHdr := header.TCP(header.IPv4(v.AsSlice()).Payload())
  1351  		iss := seqnum.Value(tcpHdr.SequenceNumber())
  1352  		tcpCheckers := []checker.TransportChecker{
  1353  			checker.SrcPort(context.StackPort),
  1354  			checker.DstPort(context.TestPort),
  1355  			checker.TCPFlags(header.TCPFlagAck | header.TCPFlagSyn),
  1356  			checker.TCPAckNum(uint32(irs) + 1),
  1357  			checker.TCPSynOptions(header.TCPSynOptions{
  1358  				WS:  tcp.FindWndScale(tcp.DefaultReceiveBufferSize),
  1359  				MSS: c.MSSWithoutOptions(),
  1360  			}),
  1361  		}
  1362  
  1363  		checker.IPv4(t, v, checker.TCP(tcpCheckers...))
  1364  
  1365  		// Send a RST to abort the handshake.
  1366  		c.SendPacket(nil, &context.Headers{
  1367  			SrcPort: context.TestPort,
  1368  			DstPort: context.StackPort,
  1369  			Flags:   header.TCPFlagRst,
  1370  			SeqNum:  irs + 1,
  1371  			AckNum:  iss + 1,
  1372  			RcvWnd:  0,
  1373  		})
  1374  
  1375  	}
  1376  
  1377  	for i := 0; i < backlog*5; i++ {
  1378  		doOne()
  1379  	}
  1380  }
  1381  
  1382  // TestTCPAckBeforeAcceptV4 tests that once the 3-way handshake is complete,
  1383  // peers can send data and expect a response within a reasonable amount of time
  1384  // without calling Accept on the listening endpoint first.
  1385  //
  1386  // This test uses IPv4.
  1387  func TestTCPAckBeforeAcceptV4(t *testing.T) {
  1388  	for _, cookieEnabled := range []tcpip.TCPAlwaysUseSynCookies{false, true} {
  1389  		t.Run(fmt.Sprintf("syn-cookies enabled: %t", cookieEnabled), func(t *testing.T) {
  1390  			c := context.New(t, e2e.DefaultMTU)
  1391  			defer c.Cleanup()
  1392  
  1393  			if err := c.Stack().SetTransportProtocolOption(header.TCPProtocolNumber, &cookieEnabled); err != nil {
  1394  				panic(fmt.Sprintf("SetTransportProtocolOption(%d, %T) = %s", header.TCPProtocolNumber, cookieEnabled, err))
  1395  			}
  1396  
  1397  			c.Create(-1)
  1398  
  1399  			if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  1400  				t.Fatal("Bind failed:", err)
  1401  			}
  1402  
  1403  			if err := c.EP.Listen(10); err != nil {
  1404  				t.Fatal("Listen failed:", err)
  1405  			}
  1406  
  1407  			irs, iss := executeHandshake(t, c, context.TestPort, bool(cookieEnabled))
  1408  
  1409  			// Send data before accepting the connection.
  1410  			c.SendPacket([]byte{1, 2, 3, 4}, &context.Headers{
  1411  				SrcPort: context.TestPort,
  1412  				DstPort: context.StackPort,
  1413  				Flags:   header.TCPFlagAck,
  1414  				SeqNum:  irs + 1,
  1415  				AckNum:  iss + 1,
  1416  			})
  1417  
  1418  			// Receive ACK for the data we sent.
  1419  			v := c.GetPacket()
  1420  			defer v.Release()
  1421  			checker.IPv4(t, v, checker.TCP(
  1422  				checker.DstPort(context.TestPort),
  1423  				checker.TCPFlags(header.TCPFlagAck),
  1424  				checker.TCPSeqNum(uint32(iss+1)),
  1425  				checker.TCPAckNum(uint32(irs+5))))
  1426  		})
  1427  	}
  1428  }
  1429  
  1430  // TestTCPAckBeforeAcceptV6 tests that once the 3-way handshake is complete,
  1431  // peers can send data and expect a response within a reasonable amount of time
  1432  // without calling Accept on the listening endpoint first.
  1433  //
  1434  // This test uses IPv6.
  1435  func TestTCPAckBeforeAcceptV6(t *testing.T) {
  1436  	for _, cookieEnabled := range []tcpip.TCPAlwaysUseSynCookies{false, true} {
  1437  		t.Run(fmt.Sprintf("syn-cookies enabled: %t", cookieEnabled), func(t *testing.T) {
  1438  			c := context.New(t, e2e.DefaultMTU)
  1439  			defer c.Cleanup()
  1440  
  1441  			if err := c.Stack().SetTransportProtocolOption(header.TCPProtocolNumber, &cookieEnabled); err != nil {
  1442  				panic(fmt.Sprintf("SetTransportProtocolOption(%d, %T) = %s", header.TCPProtocolNumber, cookieEnabled, err))
  1443  			}
  1444  			c.CreateV6Endpoint(true)
  1445  
  1446  			if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  1447  				t.Fatal("Bind failed:", err)
  1448  			}
  1449  
  1450  			if err := c.EP.Listen(10); err != nil {
  1451  				t.Fatal("Listen failed:", err)
  1452  			}
  1453  
  1454  			irs, iss := executeV6Handshake(t, c, context.TestPort, bool(cookieEnabled))
  1455  
  1456  			// Send data before accepting the connection.
  1457  			c.SendV6Packet([]byte{1, 2, 3, 4}, &context.Headers{
  1458  				SrcPort: context.TestPort,
  1459  				DstPort: context.StackPort,
  1460  				Flags:   header.TCPFlagAck,
  1461  				SeqNum:  irs + 1,
  1462  				AckNum:  iss + 1,
  1463  			})
  1464  
  1465  			// Receive ACK for the data we sent.
  1466  			v := c.GetV6Packet()
  1467  			defer v.Release()
  1468  			checker.IPv6(t, v, checker.TCP(
  1469  				checker.DstPort(context.TestPort),
  1470  				checker.TCPFlags(header.TCPFlagAck),
  1471  				checker.TCPSeqNum(uint32(iss+1)),
  1472  				checker.TCPAckNum(uint32(irs+5))))
  1473  		})
  1474  	}
  1475  }
  1476  
  1477  func TestSendRstOnListenerRxAckV4(t *testing.T) {
  1478  	c := context.New(t, e2e.DefaultMTU)
  1479  	defer c.Cleanup()
  1480  
  1481  	c.Create(-1 /* epRcvBuf */)
  1482  
  1483  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  1484  		t.Fatal("Bind failed:", err)
  1485  	}
  1486  
  1487  	if err := c.EP.Listen(10 /* backlog */); err != nil {
  1488  		t.Fatal("Listen failed:", err)
  1489  	}
  1490  
  1491  	c.SendPacket(nil, &context.Headers{
  1492  		SrcPort: context.TestPort,
  1493  		DstPort: context.StackPort,
  1494  		Flags:   header.TCPFlagFin | header.TCPFlagAck,
  1495  		SeqNum:  100,
  1496  		AckNum:  200,
  1497  	})
  1498  
  1499  	v := c.GetPacket()
  1500  	defer v.Release()
  1501  	checker.IPv4(t, v, checker.TCP(
  1502  		checker.DstPort(context.TestPort),
  1503  		checker.TCPFlags(header.TCPFlagRst),
  1504  		checker.TCPSeqNum(200)))
  1505  }
  1506  
  1507  func TestSendRstOnListenerRxAckV6(t *testing.T) {
  1508  	c := context.New(t, e2e.DefaultMTU)
  1509  	defer c.Cleanup()
  1510  
  1511  	c.CreateV6Endpoint(true /* v6Only */)
  1512  
  1513  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  1514  		t.Fatal("Bind failed:", err)
  1515  	}
  1516  
  1517  	if err := c.EP.Listen(10 /* backlog */); err != nil {
  1518  		t.Fatal("Listen failed:", err)
  1519  	}
  1520  
  1521  	c.SendV6Packet(nil, &context.Headers{
  1522  		SrcPort: context.TestPort,
  1523  		DstPort: context.StackPort,
  1524  		Flags:   header.TCPFlagFin | header.TCPFlagAck,
  1525  		SeqNum:  100,
  1526  		AckNum:  200,
  1527  	})
  1528  
  1529  	v := c.GetV6Packet()
  1530  	defer v.Release()
  1531  	checker.IPv6(t, v, checker.TCP(
  1532  		checker.DstPort(context.TestPort),
  1533  		checker.TCPFlags(header.TCPFlagRst),
  1534  		checker.TCPSeqNum(200)))
  1535  }
  1536  
  1537  // TestListenShutdown tests for the listening endpoint replying with RST
  1538  // on read shutdown.
  1539  func TestListenShutdown(t *testing.T) {
  1540  	c := context.New(t, e2e.DefaultMTU)
  1541  	defer c.Cleanup()
  1542  
  1543  	c.Create(-1 /* epRcvBuf */)
  1544  
  1545  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  1546  		t.Fatal("Bind failed:", err)
  1547  	}
  1548  
  1549  	if err := c.EP.Listen(1 /* backlog */); err != nil {
  1550  		t.Fatal("Listen failed:", err)
  1551  	}
  1552  
  1553  	if err := c.EP.Shutdown(tcpip.ShutdownRead); err != nil {
  1554  		t.Fatal("Shutdown failed:", err)
  1555  	}
  1556  
  1557  	c.SendPacket(nil, &context.Headers{
  1558  		SrcPort: context.TestPort,
  1559  		DstPort: context.StackPort,
  1560  		Flags:   header.TCPFlagSyn,
  1561  		SeqNum:  100,
  1562  		AckNum:  200,
  1563  	})
  1564  
  1565  	// Expect the listening endpoint to reset the connection.
  1566  
  1567  	v := c.GetPacket()
  1568  	defer v.Release()
  1569  	checker.IPv4(t, v, checker.TCP(
  1570  		checker.DstPort(context.TestPort),
  1571  		checker.TCPFlags(header.TCPFlagAck|header.TCPFlagRst),
  1572  	))
  1573  }
  1574  
  1575  func TestListenerReadinessOnEvent(t *testing.T) {
  1576  	s := stack.New(stack.Options{
  1577  		TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol},
  1578  		NetworkProtocols:   []stack.NetworkProtocolFactory{ipv4.NewProtocol},
  1579  	})
  1580  	defer s.Destroy()
  1581  	{
  1582  		ep := loopback.New()
  1583  		if testing.Verbose() {
  1584  			ep = sniffer.New(ep)
  1585  		}
  1586  		const id = 1
  1587  		if err := s.CreateNIC(id, ep); err != nil {
  1588  			t.Fatalf("CreateNIC(%d, %T): %s", id, ep, err)
  1589  		}
  1590  		protocolAddr := tcpip.ProtocolAddress{
  1591  			Protocol:          ipv4.ProtocolNumber,
  1592  			AddressWithPrefix: tcpip.Address(context.StackAddr).WithPrefix(),
  1593  		}
  1594  		if err := s.AddProtocolAddress(id, protocolAddr, stack.AddressProperties{}); err != nil {
  1595  			t.Fatalf("AddProtocolAddress(%d, %+v, {}): %s", id, protocolAddr, err)
  1596  		}
  1597  		s.SetRouteTable([]tcpip.Route{
  1598  			{Destination: header.IPv4EmptySubnet, NIC: id},
  1599  		})
  1600  	}
  1601  
  1602  	var wq waiter.Queue
  1603  	ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq)
  1604  	if err != nil {
  1605  		t.Fatalf("NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, _): %s", err)
  1606  	}
  1607  	defer ep.Close()
  1608  
  1609  	if err := ep.Bind(tcpip.FullAddress{Addr: context.StackAddr}); err != nil {
  1610  		t.Fatalf("Bind(%s): %s", context.StackAddr, err)
  1611  	}
  1612  	const backlog = 1
  1613  	if err := ep.Listen(backlog); err != nil {
  1614  		t.Fatalf("Listen(%d): %s", backlog, err)
  1615  	}
  1616  
  1617  	address, err := ep.GetLocalAddress()
  1618  	if err != nil {
  1619  		t.Fatalf("GetLocalAddress(): %s", err)
  1620  	}
  1621  
  1622  	conn, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq)
  1623  	if err != nil {
  1624  		t.Fatalf("NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, _): %s", err)
  1625  	}
  1626  	defer conn.Close()
  1627  
  1628  	events := make(chan waiter.EventMask)
  1629  	// Scope `entry` to allow a binding of the same name below.
  1630  	{
  1631  		entry := waiter.NewFunctionEntry(waiter.EventIn, func(mask waiter.EventMask) {
  1632  			events <- ep.Readiness(mask)
  1633  		})
  1634  		wq.EventRegister(&entry)
  1635  		defer wq.EventUnregister(&entry)
  1636  	}
  1637  
  1638  	entry, ch := waiter.NewChannelEntry(waiter.EventOut)
  1639  	wq.EventRegister(&entry)
  1640  	defer wq.EventUnregister(&entry)
  1641  
  1642  	switch err := conn.Connect(address).(type) {
  1643  	case *tcpip.ErrConnectStarted:
  1644  	default:
  1645  		t.Fatalf("Connect(%#v): %v", address, err)
  1646  	}
  1647  
  1648  	// Read at least one event.
  1649  	got := <-events
  1650  	for {
  1651  		select {
  1652  		case event := <-events:
  1653  			got |= event
  1654  			continue
  1655  		case <-ch:
  1656  			if want := waiter.ReadableEvents; got != want {
  1657  				t.Errorf("observed events = %b, want %b", got, want)
  1658  			}
  1659  		}
  1660  		break
  1661  	}
  1662  }
  1663  
  1664  // TestListenCloseWhileConnect tests for the listening endpoint to
  1665  // drain the accept-queue when closed. This should reset all of the
  1666  // pending connections that are waiting to be accepted.
  1667  func TestListenCloseWhileConnect(t *testing.T) {
  1668  	c := context.New(t, e2e.DefaultMTU)
  1669  	defer c.Cleanup()
  1670  
  1671  	c.Create(-1 /* epRcvBuf */)
  1672  
  1673  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  1674  		t.Fatal("Bind failed:", err)
  1675  	}
  1676  
  1677  	if err := c.EP.Listen(1 /* backlog */); err != nil {
  1678  		t.Fatal("Listen failed:", err)
  1679  	}
  1680  
  1681  	waitEntry, notifyCh := waiter.NewChannelEntry(waiter.ReadableEvents)
  1682  	c.WQ.EventRegister(&waitEntry)
  1683  	defer c.WQ.EventUnregister(&waitEntry)
  1684  
  1685  	executeHandshake(t, c, context.TestPort, true /* synCookiesInUse */)
  1686  	// Wait for the new endpoint created because of handshake to be delivered
  1687  	// to the listening endpoint's accept queue.
  1688  	<-notifyCh
  1689  
  1690  	// Close the listening endpoint.
  1691  	c.EP.Close()
  1692  
  1693  	// Expect the listening endpoint to reset the connection.
  1694  	v := c.GetPacket()
  1695  	defer v.Release()
  1696  	checker.IPv4(t, v, checker.TCP(
  1697  		checker.DstPort(context.TestPort),
  1698  		checker.TCPFlags(header.TCPFlagAck|header.TCPFlagRst),
  1699  	))
  1700  }
  1701  
  1702  func TestTOSV4(t *testing.T) {
  1703  	c := context.New(t, e2e.DefaultMTU)
  1704  	defer c.Cleanup()
  1705  
  1706  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  1707  	if err != nil {
  1708  		t.Fatalf("NewEndpoint failed: %s", err)
  1709  	}
  1710  	c.EP = ep
  1711  
  1712  	const tos = 0xC0
  1713  	if err := c.EP.SetSockOptInt(tcpip.IPv4TOSOption, tos); err != nil {
  1714  		t.Errorf("SetSockOptInt(IPv4TOSOption, %d) failed: %s", tos, err)
  1715  	}
  1716  
  1717  	v, err := c.EP.GetSockOptInt(tcpip.IPv4TOSOption)
  1718  	if err != nil {
  1719  		t.Errorf("GetSockoptInt(IPv4TOSOption) failed: %s", err)
  1720  	}
  1721  
  1722  	if v != tos {
  1723  		t.Errorf("got GetSockOptInt(IPv4TOSOption) = %d, want = %d", v, tos)
  1724  	}
  1725  
  1726  	e2e.TestV4Connect(t, c, checker.TOS(tos, 0))
  1727  
  1728  	data := []byte{1, 2, 3}
  1729  	var r bytes.Reader
  1730  	r.Reset(data)
  1731  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  1732  		t.Fatalf("Write failed: %s", err)
  1733  	}
  1734  
  1735  	// Check that data is received.
  1736  	p := c.GetPacket()
  1737  	defer p.Release()
  1738  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  1739  	checker.IPv4(t, p,
  1740  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  1741  		checker.TCP(
  1742  			checker.DstPort(context.TestPort),
  1743  			checker.TCPSeqNum(uint32(c.IRS)+1),
  1744  			checker.TCPAckNum(uint32(iss)), // Acknum is initial sequence number + 1
  1745  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  1746  		),
  1747  		checker.TOS(tos, 0),
  1748  	)
  1749  
  1750  	if b := p.AsSlice()[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(data, b) {
  1751  		t.Errorf("got data = %x, want = %x", p.AsSlice(), data)
  1752  	}
  1753  }
  1754  
  1755  func TestTrafficClassV6(t *testing.T) {
  1756  	c := context.New(t, e2e.DefaultMTU)
  1757  	defer c.Cleanup()
  1758  
  1759  	c.CreateV6Endpoint(false)
  1760  
  1761  	const tos = 0xC0
  1762  	if err := c.EP.SetSockOptInt(tcpip.IPv6TrafficClassOption, tos); err != nil {
  1763  		t.Errorf("SetSockOpInt(IPv6TrafficClassOption, %d) failed: %s", tos, err)
  1764  	}
  1765  
  1766  	v, err := c.EP.GetSockOptInt(tcpip.IPv6TrafficClassOption)
  1767  	if err != nil {
  1768  		t.Fatalf("GetSockoptInt(IPv6TrafficClassOption) failed: %s", err)
  1769  	}
  1770  
  1771  	if v != tos {
  1772  		t.Errorf("got GetSockOptInt(IPv6TrafficClassOption) = %d, want = %d", v, tos)
  1773  	}
  1774  
  1775  	// Test the connection request.
  1776  	e2e.TestV6Connect(t, c, checker.TOS(tos, 0))
  1777  
  1778  	data := []byte{1, 2, 3}
  1779  	var r bytes.Reader
  1780  	r.Reset(data)
  1781  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  1782  		t.Fatalf("Write failed: %s", err)
  1783  	}
  1784  
  1785  	// Check that data is received.
  1786  	b := c.GetV6Packet()
  1787  	defer b.Release()
  1788  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  1789  	checker.IPv6(t, b,
  1790  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  1791  		checker.TCP(
  1792  			checker.DstPort(context.TestPort),
  1793  			checker.TCPSeqNum(uint32(c.IRS)+1),
  1794  			checker.TCPAckNum(uint32(iss)),
  1795  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  1796  		),
  1797  		checker.TOS(tos, 0),
  1798  	)
  1799  
  1800  	if p := b.AsSlice()[header.IPv6MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(data, p) {
  1801  		t.Errorf("got data = %x, want = %x", p, data)
  1802  	}
  1803  }
  1804  
  1805  func TestConnectBindToDevice(t *testing.T) {
  1806  	for _, test := range []struct {
  1807  		name   string
  1808  		device tcpip.NICID
  1809  		want   tcp.EndpointState
  1810  	}{
  1811  		{"RightDevice", 1, tcp.StateEstablished},
  1812  		{"WrongDevice", 2, tcp.StateSynSent},
  1813  		{"AnyDevice", 0, tcp.StateEstablished},
  1814  	} {
  1815  		t.Run(test.name, func(t *testing.T) {
  1816  			c := context.New(t, e2e.DefaultMTU)
  1817  			defer c.Cleanup()
  1818  
  1819  			c.Create(-1)
  1820  			if err := c.EP.SocketOptions().SetBindToDevice(int32(test.device)); err != nil {
  1821  				t.Fatalf("c.EP.SetSockOpt(&%T(%d)): %s", test.device, test.device, err)
  1822  			}
  1823  			// Start connection attempt.
  1824  			waitEntry, _ := waiter.NewChannelEntry(waiter.WritableEvents)
  1825  			c.WQ.EventRegister(&waitEntry)
  1826  			defer c.WQ.EventUnregister(&waitEntry)
  1827  
  1828  			err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort})
  1829  			if d := cmp.Diff(&tcpip.ErrConnectStarted{}, err); d != "" {
  1830  				t.Fatalf("c.EP.Connect(...) mismatch (-want +got):\n%s", d)
  1831  			}
  1832  
  1833  			// Receive SYN packet.
  1834  			v := c.GetPacket()
  1835  			defer v.Release()
  1836  			checker.IPv4(t, v,
  1837  				checker.TCP(
  1838  					checker.DstPort(context.TestPort),
  1839  					checker.TCPFlags(header.TCPFlagSyn),
  1840  				),
  1841  			)
  1842  			if got, want := tcp.EndpointState(c.EP.State()), tcp.StateSynSent; got != want {
  1843  				t.Fatalf("unexpected endpoint state: want %s, got %s", want, got)
  1844  			}
  1845  			tcpHdr := header.TCP(header.IPv4(v.AsSlice()).Payload())
  1846  			c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
  1847  
  1848  			iss := seqnum.Value(context.TestInitialSequenceNumber)
  1849  			rcvWnd := seqnum.Size(30000)
  1850  			c.SendPacket(nil, &context.Headers{
  1851  				SrcPort: tcpHdr.DestinationPort(),
  1852  				DstPort: tcpHdr.SourcePort(),
  1853  				Flags:   header.TCPFlagSyn | header.TCPFlagAck,
  1854  				SeqNum:  iss,
  1855  				AckNum:  c.IRS.Add(1),
  1856  				RcvWnd:  rcvWnd,
  1857  				TCPOpts: nil,
  1858  			})
  1859  
  1860  			v = c.GetPacket()
  1861  			defer v.Release()
  1862  			if got, want := tcp.EndpointState(c.EP.State()), test.want; got != want {
  1863  				t.Fatalf("unexpected endpoint state: want %s, got %s", want, got)
  1864  			}
  1865  		})
  1866  	}
  1867  }
  1868  
  1869  func TestShutdownConnectingSocket(t *testing.T) {
  1870  	for _, test := range []struct {
  1871  		name         string
  1872  		shutdownMode tcpip.ShutdownFlags
  1873  	}{
  1874  		{"ShutdownRead", tcpip.ShutdownRead},
  1875  		{"ShutdownWrite", tcpip.ShutdownWrite},
  1876  		{"ShutdownReadWrite", tcpip.ShutdownRead | tcpip.ShutdownWrite},
  1877  	} {
  1878  		t.Run(test.name, func(t *testing.T) {
  1879  			c := context.New(t, e2e.DefaultMTU)
  1880  			defer c.Cleanup()
  1881  
  1882  			// Create an endpoint, don't handshake because we want to interfere with
  1883  			// the handshake process.
  1884  			c.Create(-1)
  1885  
  1886  			waitEntry, ch := waiter.NewChannelEntry(waiter.EventHUp)
  1887  			c.WQ.EventRegister(&waitEntry)
  1888  			defer c.WQ.EventUnregister(&waitEntry)
  1889  
  1890  			// Start connection attempt.
  1891  			addr := tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}
  1892  			if d := cmp.Diff(&tcpip.ErrConnectStarted{}, c.EP.Connect(addr)); d != "" {
  1893  				t.Fatalf("Connect(...) mismatch (-want +got):\n%s", d)
  1894  			}
  1895  
  1896  			// Check the SYN packet.
  1897  			v := c.GetPacket()
  1898  			defer v.Release()
  1899  			checker.IPv4(t, v,
  1900  				checker.TCP(
  1901  					checker.DstPort(context.TestPort),
  1902  					checker.TCPFlags(header.TCPFlagSyn),
  1903  				),
  1904  			)
  1905  
  1906  			if got, want := tcp.EndpointState(c.EP.State()), tcp.StateSynSent; got != want {
  1907  				t.Fatalf("got State() = %s, want %s", got, want)
  1908  			}
  1909  
  1910  			if err := c.EP.Shutdown(test.shutdownMode); err != nil {
  1911  				t.Fatalf("Shutdown failed: %s", err)
  1912  			}
  1913  
  1914  			// The endpoint internal state is updated immediately.
  1915  			if got, want := tcp.EndpointState(c.EP.State()), tcp.StateError; got != want {
  1916  				t.Fatalf("got State() = %s, want %s", got, want)
  1917  			}
  1918  
  1919  			select {
  1920  			case <-ch:
  1921  			default:
  1922  				t.Fatal("endpoint was not notified")
  1923  			}
  1924  
  1925  			ept := endpointTester{c.EP}
  1926  			ept.CheckReadError(t, &tcpip.ErrConnectionReset{})
  1927  
  1928  			// If the endpoint is not properly shutdown, it'll re-attempt to connect
  1929  			// by sending another ACK packet.
  1930  			c.CheckNoPacketTimeout("got an unexpected packet", tcp.InitialRTO+(500*time.Millisecond))
  1931  		})
  1932  	}
  1933  }
  1934  
  1935  func TestSynSent(t *testing.T) {
  1936  	for _, test := range []struct {
  1937  		name  string
  1938  		reset bool
  1939  	}{
  1940  		{"RstOnSynSent", true},
  1941  		{"CloseOnSynSent", false},
  1942  	} {
  1943  		t.Run(test.name, func(t *testing.T) {
  1944  			c := context.New(t, e2e.DefaultMTU)
  1945  			defer c.Cleanup()
  1946  
  1947  			// Create an endpoint, don't handshake because we want to interfere with the
  1948  			// handshake process.
  1949  			c.Create(-1)
  1950  
  1951  			// Start connection attempt.
  1952  			waitEntry, ch := waiter.NewChannelEntry(waiter.EventHUp)
  1953  			c.WQ.EventRegister(&waitEntry)
  1954  			defer c.WQ.EventUnregister(&waitEntry)
  1955  
  1956  			addr := tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}
  1957  			err := c.EP.Connect(addr)
  1958  			if d := cmp.Diff(&tcpip.ErrConnectStarted{}, err); d != "" {
  1959  				t.Fatalf("Connect(...) mismatch (-want +got):\n%s", d)
  1960  			}
  1961  
  1962  			// Receive SYN packet.
  1963  			v := c.GetPacket()
  1964  			defer v.Release()
  1965  			checker.IPv4(t, v,
  1966  				checker.TCP(
  1967  					checker.DstPort(context.TestPort),
  1968  					checker.TCPFlags(header.TCPFlagSyn),
  1969  				),
  1970  			)
  1971  
  1972  			if got, want := tcp.EndpointState(c.EP.State()), tcp.StateSynSent; got != want {
  1973  				t.Fatalf("got State() = %s, want %s", got, want)
  1974  			}
  1975  			tcpHdr := header.TCP(header.IPv4(v.AsSlice()).Payload())
  1976  			c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
  1977  
  1978  			if test.reset {
  1979  				// Send a packet with a proper ACK and a RST flag to cause the socket
  1980  				// to error and close out.
  1981  				iss := seqnum.Value(context.TestInitialSequenceNumber)
  1982  				rcvWnd := seqnum.Size(30000)
  1983  				c.SendPacket(nil, &context.Headers{
  1984  					SrcPort: tcpHdr.DestinationPort(),
  1985  					DstPort: tcpHdr.SourcePort(),
  1986  					Flags:   header.TCPFlagRst | header.TCPFlagAck,
  1987  					SeqNum:  iss,
  1988  					AckNum:  c.IRS.Add(1),
  1989  					RcvWnd:  rcvWnd,
  1990  					TCPOpts: nil,
  1991  				})
  1992  			} else {
  1993  				c.EP.Close()
  1994  			}
  1995  
  1996  			// Wait for receive to be notified.
  1997  			select {
  1998  			case <-ch:
  1999  			case <-time.After(3 * time.Second):
  2000  				t.Fatal("timed out waiting for packet to arrive")
  2001  			}
  2002  
  2003  			ept := endpointTester{c.EP}
  2004  			if test.reset {
  2005  				ept.CheckReadError(t, &tcpip.ErrConnectionRefused{})
  2006  			} else {
  2007  				ept.CheckReadError(t, &tcpip.ErrAborted{})
  2008  			}
  2009  
  2010  			if got := c.Stack().Stats().TCP.CurrentConnected.Value(); got != 0 {
  2011  				t.Errorf("got stats.TCP.CurrentConnected.Value() = %d, want = 0", got)
  2012  			}
  2013  
  2014  			// Due to the RST the endpoint should be in an error state.
  2015  			if got, want := tcp.EndpointState(c.EP.State()), tcp.StateError; got != want {
  2016  				t.Fatalf("got State() = %s, want %s", got, want)
  2017  			}
  2018  		})
  2019  	}
  2020  }
  2021  
  2022  func TestOutOfOrderReceive(t *testing.T) {
  2023  	c := context.New(t, e2e.DefaultMTU)
  2024  	defer c.Cleanup()
  2025  
  2026  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  2027  
  2028  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  2029  	c.WQ.EventRegister(&we)
  2030  	defer c.WQ.EventUnregister(&we)
  2031  
  2032  	ept := endpointTester{c.EP}
  2033  	ept.CheckReadError(t, &tcpip.ErrWouldBlock{})
  2034  
  2035  	// Send second half of data first, with seqnum 3 ahead of expected.
  2036  	data := []byte{1, 2, 3, 4, 5, 6}
  2037  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  2038  	c.SendPacket(data[3:], &context.Headers{
  2039  		SrcPort: context.TestPort,
  2040  		DstPort: c.Port,
  2041  		Flags:   header.TCPFlagAck,
  2042  		SeqNum:  iss.Add(3),
  2043  		AckNum:  c.IRS.Add(1),
  2044  		RcvWnd:  30000,
  2045  	})
  2046  
  2047  	// Check that we get an ACK specifying which seqnum is expected.
  2048  	v := c.GetPacket()
  2049  	defer v.Release()
  2050  	checker.IPv4(t, v, checker.TCP(
  2051  		checker.DstPort(context.TestPort),
  2052  		checker.TCPSeqNum(uint32(c.IRS)+1),
  2053  		checker.TCPAckNum(uint32(iss)),
  2054  		checker.TCPFlags(header.TCPFlagAck),
  2055  	),
  2056  	)
  2057  
  2058  	// Wait 200ms and check that no data has been received.
  2059  	time.Sleep(200 * time.Millisecond)
  2060  	ept.CheckReadError(t, &tcpip.ErrWouldBlock{})
  2061  
  2062  	// Send the first 3 bytes now.
  2063  	c.SendPacket(data[:3], &context.Headers{
  2064  		SrcPort: context.TestPort,
  2065  		DstPort: c.Port,
  2066  		Flags:   header.TCPFlagAck,
  2067  		SeqNum:  iss,
  2068  		AckNum:  c.IRS.Add(1),
  2069  		RcvWnd:  30000,
  2070  	})
  2071  
  2072  	// Receive data.
  2073  	read := ept.CheckReadFull(t, 6, ch, 5*time.Second)
  2074  
  2075  	// Check that we received the data in proper order.
  2076  	if !bytes.Equal(data, read) {
  2077  		t.Fatalf("got data = %v, want = %v", read, data)
  2078  	}
  2079  
  2080  	// Check that the whole data is acknowledged.
  2081  	v = c.GetPacket()
  2082  	defer v.Release()
  2083  	checker.IPv4(t, v, checker.TCP(
  2084  		checker.DstPort(context.TestPort),
  2085  		checker.TCPSeqNum(uint32(c.IRS)+1),
  2086  		checker.TCPAckNum(uint32(iss)+uint32(len(data))),
  2087  		checker.TCPFlags(header.TCPFlagAck),
  2088  	),
  2089  	)
  2090  }
  2091  
  2092  func TestOutOfOrderFlood(t *testing.T) {
  2093  	c := context.New(t, e2e.DefaultMTU)
  2094  	defer c.Cleanup()
  2095  
  2096  	rcvBufSz := math.MaxUint16
  2097  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, rcvBufSz)
  2098  
  2099  	ept := endpointTester{c.EP}
  2100  	ept.CheckReadError(t, &tcpip.ErrWouldBlock{})
  2101  
  2102  	// Send 100 packets with seqnum iss + 6 before the actual one that is
  2103  	// expected.
  2104  	data := []byte{1, 2, 3, 4, 5, 6}
  2105  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  2106  	for i := 0; i < 100; i++ {
  2107  		c.SendPacket(data[3:], &context.Headers{
  2108  			SrcPort: context.TestPort,
  2109  			DstPort: c.Port,
  2110  			Flags:   header.TCPFlagAck,
  2111  			SeqNum:  iss.Add(6),
  2112  			AckNum:  c.IRS.Add(1),
  2113  			RcvWnd:  30000,
  2114  		})
  2115  
  2116  		v := c.GetPacket()
  2117  		defer v.Release()
  2118  		checker.IPv4(t, v, checker.TCP(
  2119  			checker.DstPort(context.TestPort),
  2120  			checker.TCPSeqNum(uint32(c.IRS)+1),
  2121  			checker.TCPAckNum(uint32(iss)),
  2122  			checker.TCPFlags(header.TCPFlagAck),
  2123  		),
  2124  		)
  2125  	}
  2126  
  2127  	// Send packet with seqnum as initial + 3. It won't be discarded
  2128  	// because the receive window limits the sender to rcvBufSize/2 bytes,
  2129  	// but we allow (3/4)*rcvBufSize to be used for out-of-order bytes. So
  2130  	// the sender hasn't filled the buffer and we still have space to
  2131  	// receive it.
  2132  	c.SendPacket(data[3:], &context.Headers{
  2133  		SrcPort: context.TestPort,
  2134  		DstPort: c.Port,
  2135  		Flags:   header.TCPFlagAck,
  2136  		SeqNum:  iss.Add(3),
  2137  		AckNum:  c.IRS.Add(1),
  2138  		RcvWnd:  30000,
  2139  	})
  2140  
  2141  	v := c.GetPacket()
  2142  	defer v.Release()
  2143  	checker.IPv4(t, v, checker.TCP(
  2144  		checker.DstPort(context.TestPort),
  2145  		checker.TCPSeqNum(uint32(c.IRS)+1),
  2146  		checker.TCPAckNum(uint32(iss)),
  2147  		checker.TCPFlags(header.TCPFlagAck),
  2148  	),
  2149  	)
  2150  
  2151  	// Now send the expected packet with initial sequence number.
  2152  	c.SendPacket(data[:3], &context.Headers{
  2153  		SrcPort: context.TestPort,
  2154  		DstPort: c.Port,
  2155  		Flags:   header.TCPFlagAck,
  2156  		SeqNum:  iss,
  2157  		AckNum:  c.IRS.Add(1),
  2158  		RcvWnd:  30000,
  2159  	})
  2160  
  2161  	// Check that all packets are acknowledged.
  2162  	v = c.GetPacket()
  2163  	defer v.Release()
  2164  	checker.IPv4(t, v, checker.TCP(
  2165  		checker.DstPort(context.TestPort),
  2166  		checker.TCPSeqNum(uint32(c.IRS)+1),
  2167  		checker.TCPAckNum(uint32(iss)+9),
  2168  		checker.TCPFlags(header.TCPFlagAck),
  2169  	),
  2170  	)
  2171  }
  2172  
  2173  func TestRstOnCloseWithUnreadData(t *testing.T) {
  2174  	c := context.New(t, e2e.DefaultMTU)
  2175  	defer c.Cleanup()
  2176  
  2177  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  2178  
  2179  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  2180  	c.WQ.EventRegister(&we)
  2181  	defer c.WQ.EventUnregister(&we)
  2182  
  2183  	ept := endpointTester{c.EP}
  2184  	ept.CheckReadError(t, &tcpip.ErrWouldBlock{})
  2185  
  2186  	data := []byte{1, 2, 3}
  2187  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  2188  	c.SendPacket(data, &context.Headers{
  2189  		SrcPort: context.TestPort,
  2190  		DstPort: c.Port,
  2191  		Flags:   header.TCPFlagAck,
  2192  		SeqNum:  iss,
  2193  		AckNum:  c.IRS.Add(1),
  2194  		RcvWnd:  30000,
  2195  	})
  2196  
  2197  	// Wait for receive to be notified.
  2198  	select {
  2199  	case <-ch:
  2200  	case <-time.After(3 * time.Second):
  2201  		t.Fatalf("Timed out waiting for data to arrive")
  2202  	}
  2203  
  2204  	// Check that ACK is received, this happens regardless of the read.
  2205  	v := c.GetPacket()
  2206  	defer v.Release()
  2207  	checker.IPv4(t, v, checker.TCP(
  2208  		checker.DstPort(context.TestPort),
  2209  		checker.TCPSeqNum(uint32(c.IRS)+1),
  2210  		checker.TCPAckNum(uint32(iss)+uint32(len(data))),
  2211  		checker.TCPFlags(header.TCPFlagAck),
  2212  	),
  2213  	)
  2214  
  2215  	// Now that we know we have unread data, let's just close the connection
  2216  	// and verify that netstack sends an RST rather than a FIN.
  2217  	c.EP.Close()
  2218  
  2219  	v = c.GetPacket()
  2220  	defer v.Release()
  2221  	checker.IPv4(t, v, checker.TCP(
  2222  		checker.DstPort(context.TestPort),
  2223  		checker.TCPFlags(header.TCPFlagAck|header.TCPFlagRst),
  2224  		// We shouldn't consume a sequence number on RST.
  2225  		checker.TCPSeqNum(uint32(c.IRS)+1),
  2226  	))
  2227  	// The RST puts the endpoint into an error state.
  2228  	if got, want := tcp.EndpointState(c.EP.State()), tcp.StateError; got != want {
  2229  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
  2230  	}
  2231  
  2232  	// This final ACK should be ignored because an ACK on a reset doesn't mean
  2233  	// anything.
  2234  	c.SendPacket(nil, &context.Headers{
  2235  		SrcPort: context.TestPort,
  2236  		DstPort: c.Port,
  2237  		Flags:   header.TCPFlagAck,
  2238  		SeqNum:  iss.Add(seqnum.Size(len(data))),
  2239  		AckNum:  c.IRS.Add(seqnum.Size(2)),
  2240  		RcvWnd:  30000,
  2241  	})
  2242  }
  2243  
  2244  func TestRstOnCloseWithUnreadDataFinConvertRst(t *testing.T) {
  2245  	c := context.New(t, e2e.DefaultMTU)
  2246  	defer c.Cleanup()
  2247  
  2248  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  2249  
  2250  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  2251  	c.WQ.EventRegister(&we)
  2252  	defer c.WQ.EventUnregister(&we)
  2253  
  2254  	ept := endpointTester{c.EP}
  2255  	ept.CheckReadError(t, &tcpip.ErrWouldBlock{})
  2256  
  2257  	data := []byte{1, 2, 3}
  2258  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  2259  	c.SendPacket(data, &context.Headers{
  2260  		SrcPort: context.TestPort,
  2261  		DstPort: c.Port,
  2262  		Flags:   header.TCPFlagAck,
  2263  		SeqNum:  iss,
  2264  		AckNum:  c.IRS.Add(1),
  2265  		RcvWnd:  30000,
  2266  	})
  2267  
  2268  	// Wait for receive to be notified.
  2269  	select {
  2270  	case <-ch:
  2271  	case <-time.After(3 * time.Second):
  2272  		t.Fatalf("Timed out waiting for data to arrive")
  2273  	}
  2274  
  2275  	// Check that ACK is received, this happens regardless of the read.
  2276  	v := c.GetPacket()
  2277  	defer v.Release()
  2278  	checker.IPv4(t, v, checker.TCP(
  2279  		checker.DstPort(context.TestPort),
  2280  		checker.TCPSeqNum(uint32(c.IRS)+1),
  2281  		checker.TCPAckNum(uint32(iss)+uint32(len(data))),
  2282  		checker.TCPFlags(header.TCPFlagAck),
  2283  	),
  2284  	)
  2285  
  2286  	// Cause a FIN to be generated.
  2287  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  2288  		t.Fatalf("Shutdown failed: %s", err)
  2289  	}
  2290  
  2291  	// Make sure we get the FIN but DON't ACK IT.
  2292  	v = c.GetPacket()
  2293  	defer v.Release()
  2294  	checker.IPv4(t, v, checker.TCP(
  2295  		checker.DstPort(context.TestPort),
  2296  		checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  2297  		checker.TCPSeqNum(uint32(c.IRS)+1),
  2298  	))
  2299  
  2300  	if got, want := tcp.EndpointState(c.EP.State()), tcp.StateFinWait1; got != want {
  2301  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
  2302  	}
  2303  
  2304  	// Cause a RST to be generated by closing the read end now since we have
  2305  	// unread data.
  2306  	if err := c.EP.Shutdown(tcpip.ShutdownRead); err != nil {
  2307  		t.Fatalf("Shutdown failed: %s", err)
  2308  	}
  2309  
  2310  	// Make sure we get the RST
  2311  	v = c.GetPacket()
  2312  	defer v.Release()
  2313  	checker.IPv4(t, v, checker.TCP(
  2314  		checker.DstPort(context.TestPort),
  2315  		checker.TCPFlags(header.TCPFlagAck|header.TCPFlagRst),
  2316  		// RST is always generated with sndNxt which if the FIN
  2317  		// has been sent will be 1 higher than the sequence
  2318  		// number of the FIN itself.
  2319  		checker.TCPSeqNum(uint32(c.IRS)+2),
  2320  	))
  2321  	// The RST puts the endpoint into an error state.
  2322  	if got, want := tcp.EndpointState(c.EP.State()), tcp.StateError; got != want {
  2323  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
  2324  	}
  2325  
  2326  	// The ACK to the FIN should now be rejected since the connection has been
  2327  	// closed by a RST.
  2328  	c.SendPacket(nil, &context.Headers{
  2329  		SrcPort: context.TestPort,
  2330  		DstPort: c.Port,
  2331  		Flags:   header.TCPFlagAck,
  2332  		SeqNum:  iss.Add(seqnum.Size(len(data))),
  2333  		AckNum:  c.IRS.Add(seqnum.Size(2)),
  2334  		RcvWnd:  30000,
  2335  	})
  2336  }
  2337  
  2338  func TestShutdownRead(t *testing.T) {
  2339  	c := context.New(t, e2e.DefaultMTU)
  2340  	defer c.Cleanup()
  2341  
  2342  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  2343  
  2344  	ept := endpointTester{c.EP}
  2345  	ept.CheckReadError(t, &tcpip.ErrWouldBlock{})
  2346  
  2347  	if err := c.EP.Shutdown(tcpip.ShutdownRead); err != nil {
  2348  		t.Fatalf("Shutdown failed: %s", err)
  2349  	}
  2350  
  2351  	ept.CheckReadError(t, &tcpip.ErrClosedForReceive{})
  2352  	var want uint64 = 1
  2353  	if got := c.EP.Stats().(*tcp.Stats).ReadErrors.ReadClosed.Value(); got != want {
  2354  		t.Fatalf("got EP stats Stats.ReadErrors.ReadClosed got %d want %d", got, want)
  2355  	}
  2356  }
  2357  
  2358  func TestFullWindowReceive(t *testing.T) {
  2359  	c := context.New(t, e2e.DefaultMTU)
  2360  	defer c.Cleanup()
  2361  
  2362  	const rcvBufSz = 10
  2363  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, rcvBufSz)
  2364  
  2365  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  2366  	c.WQ.EventRegister(&we)
  2367  	defer c.WQ.EventUnregister(&we)
  2368  
  2369  	ept := endpointTester{c.EP}
  2370  	ept.CheckReadError(t, &tcpip.ErrWouldBlock{})
  2371  
  2372  	// Fill up the window w/ tcp.SegOverheadFactor*rcvBufSz as netstack multiplies
  2373  	// the provided buffer value by tcp.SegOverheadFactor to calculate the actual
  2374  	// receive buffer size.
  2375  	data := make([]byte, tcp.SegOverheadFactor*rcvBufSz)
  2376  	for i := range data {
  2377  		data[i] = byte(i % 255)
  2378  	}
  2379  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  2380  	c.SendPacket(data, &context.Headers{
  2381  		SrcPort: context.TestPort,
  2382  		DstPort: c.Port,
  2383  		Flags:   header.TCPFlagAck,
  2384  		SeqNum:  iss,
  2385  		AckNum:  c.IRS.Add(1),
  2386  		RcvWnd:  30000,
  2387  	})
  2388  
  2389  	// Wait for receive to be notified.
  2390  	select {
  2391  	case <-ch:
  2392  	case <-time.After(5 * time.Second):
  2393  		t.Fatalf("Timed out waiting for data to arrive")
  2394  	}
  2395  
  2396  	// Check that data is acknowledged, and window goes to zero.
  2397  	b := c.GetPacket()
  2398  	defer b.Release()
  2399  	checker.IPv4(t, b, checker.TCP(
  2400  		checker.DstPort(context.TestPort),
  2401  		checker.TCPSeqNum(uint32(c.IRS)+1),
  2402  		checker.TCPAckNum(uint32(iss)+uint32(len(data))),
  2403  		checker.TCPFlags(header.TCPFlagAck),
  2404  		checker.TCPWindow(0),
  2405  	),
  2406  	)
  2407  
  2408  	// Receive data and check it.
  2409  	v := ept.CheckRead(t)
  2410  	if !bytes.Equal(data, v) {
  2411  		t.Fatalf("got data = %v, want = %v", v, data)
  2412  	}
  2413  
  2414  	var want uint64 = 1
  2415  	if got := c.EP.Stats().(*tcp.Stats).ReceiveErrors.ZeroRcvWindowState.Value(); got != want {
  2416  		t.Fatalf("got EP stats ReceiveErrors.ZeroRcvWindowState got %d want %d", got, want)
  2417  	}
  2418  
  2419  	// Check that we get an ACK for the newly non-zero window.
  2420  	b = c.GetPacket()
  2421  	defer b.Release()
  2422  	checker.IPv4(t, b, checker.TCP(
  2423  		checker.DstPort(context.TestPort),
  2424  		checker.TCPSeqNum(uint32(c.IRS)+1),
  2425  		checker.TCPAckNum(uint32(iss)+uint32(len(data))),
  2426  		checker.TCPFlags(header.TCPFlagAck),
  2427  		checker.TCPWindow(10),
  2428  	),
  2429  	)
  2430  }
  2431  
  2432  func TestSmallReceiveBufferReadiness(t *testing.T) {
  2433  	s := stack.New(stack.Options{
  2434  		NetworkProtocols:   []stack.NetworkProtocolFactory{ipv4.NewProtocol},
  2435  		TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol},
  2436  	})
  2437  	defer s.Destroy()
  2438  
  2439  	ep := loopback.New()
  2440  	if testing.Verbose() {
  2441  		ep = sniffer.New(ep)
  2442  	}
  2443  
  2444  	const nicID = 1
  2445  	nicOpts := stack.NICOptions{Name: "nic1"}
  2446  	if err := s.CreateNICWithOptions(nicID, ep, nicOpts); err != nil {
  2447  		t.Fatalf("CreateNICWithOptions(_, _, %+v) failed: %s", nicOpts, err)
  2448  	}
  2449  
  2450  	protocolAddr := tcpip.ProtocolAddress{
  2451  		Protocol: ipv4.ProtocolNumber,
  2452  		AddressWithPrefix: tcpip.AddressWithPrefix{
  2453  			Address:   tcpip.AddrFromSlice([]byte("\x7f\x00\x00\x01")),
  2454  			PrefixLen: 32,
  2455  		},
  2456  	}
  2457  	if err := s.AddProtocolAddress(nicID, protocolAddr, stack.AddressProperties{}); err != nil {
  2458  		t.Fatalf("AddProtocolAddress(%d, %+v, {}) failed: %s", nicID, protocolAddr, err)
  2459  	}
  2460  
  2461  	{
  2462  		subnet, err := tcpip.NewSubnet(tcpip.AddrFromSlice([]byte("\x7f\x00\x00\x00")), tcpip.MaskFrom("\xff\x00\x00\x00"))
  2463  		if err != nil {
  2464  			t.Fatalf("tcpip.NewSubnet failed: %s", err)
  2465  		}
  2466  		s.SetRouteTable([]tcpip.Route{
  2467  			{
  2468  				Destination: subnet,
  2469  				NIC:         nicID,
  2470  			},
  2471  		})
  2472  	}
  2473  
  2474  	listenerEntry, listenerCh := waiter.NewChannelEntry(waiter.ReadableEvents)
  2475  	var listenerWQ waiter.Queue
  2476  	listener, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &listenerWQ)
  2477  	if err != nil {
  2478  		t.Fatalf("NewEndpoint failed: %s", err)
  2479  	}
  2480  	defer listener.Close()
  2481  	listenerWQ.EventRegister(&listenerEntry)
  2482  	defer listenerWQ.EventUnregister(&listenerEntry)
  2483  
  2484  	if err := listener.Bind(tcpip.FullAddress{}); err != nil {
  2485  		t.Fatalf("Bind failed: %s", err)
  2486  	}
  2487  	if err := listener.Listen(1); err != nil {
  2488  		t.Fatalf("Bind failed: %s", err)
  2489  	}
  2490  
  2491  	localAddress, err := listener.GetLocalAddress()
  2492  	if err != nil {
  2493  		t.Fatalf("GetLocalAddress failed: %s", err)
  2494  	}
  2495  
  2496  	for i := 8; i > 0; i /= 2 {
  2497  		size := int64(i << 12)
  2498  		t.Run(fmt.Sprintf("size=%d", size), func(t *testing.T) {
  2499  			var clientWQ waiter.Queue
  2500  			client, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &clientWQ)
  2501  			if err != nil {
  2502  				t.Fatalf("NewEndpoint failed: %s", err)
  2503  			}
  2504  			defer client.Close()
  2505  			switch err := client.Connect(localAddress).(type) {
  2506  			case nil:
  2507  				t.Fatal("Connect returned nil error")
  2508  			case *tcpip.ErrConnectStarted:
  2509  			default:
  2510  				t.Fatalf("Connect failed: %s", err)
  2511  			}
  2512  
  2513  			<-listenerCh
  2514  			server, serverWQ, err := listener.Accept(nil)
  2515  			if err != nil {
  2516  				t.Fatalf("Accept failed: %s", err)
  2517  			}
  2518  			defer server.Close()
  2519  
  2520  			client.SocketOptions().SetReceiveBufferSize(size, true)
  2521  			// Send buffer size doesn't seem to affect this test.
  2522  			// server.SocketOptions().SetSendBufferSize(size, true)
  2523  
  2524  			clientEntry, clientCh := waiter.NewChannelEntry(waiter.ReadableEvents)
  2525  			clientWQ.EventRegister(&clientEntry)
  2526  			defer clientWQ.EventUnregister(&clientEntry)
  2527  
  2528  			serverEntry, serverCh := waiter.NewChannelEntry(waiter.WritableEvents)
  2529  			serverWQ.EventRegister(&serverEntry)
  2530  			defer serverWQ.EventUnregister(&serverEntry)
  2531  
  2532  			var total int64
  2533  			for {
  2534  				var b [64 << 10]byte
  2535  				var r bytes.Reader
  2536  				r.Reset(b[:])
  2537  				switch n, err := server.Write(&r, tcpip.WriteOptions{}); err.(type) {
  2538  				case nil:
  2539  					t.Logf("wrote %d bytes", n)
  2540  					total += n
  2541  					continue
  2542  				case *tcpip.ErrWouldBlock:
  2543  					select {
  2544  					case <-serverCh:
  2545  						continue
  2546  					case <-time.After(100 * time.Millisecond):
  2547  						// Well and truly full.
  2548  						t.Logf("send and receive queues are full")
  2549  					}
  2550  				default:
  2551  					t.Fatalf("Write failed: %s", err)
  2552  				}
  2553  				break
  2554  			}
  2555  			t.Logf("wrote %d bytes in total", total)
  2556  
  2557  			var wg sync.WaitGroup
  2558  			defer wg.Wait()
  2559  
  2560  			wg.Add(2)
  2561  			go func() {
  2562  				defer wg.Done()
  2563  
  2564  				var b [64 << 10]byte
  2565  				var r bytes.Reader
  2566  				r.Reset(b[:])
  2567  				if err := func() error {
  2568  					var total int64
  2569  					defer t.Logf("wrote %d bytes in total", total)
  2570  					for r.Len() != 0 {
  2571  						switch n, err := server.Write(&r, tcpip.WriteOptions{}); err.(type) {
  2572  						case nil:
  2573  							t.Logf("wrote %d bytes", n)
  2574  							total += n
  2575  						case *tcpip.ErrWouldBlock:
  2576  							for {
  2577  								t.Logf("waiting on server")
  2578  								select {
  2579  								case <-serverCh:
  2580  								case <-time.After(time.Second):
  2581  									if readiness := server.Readiness(waiter.WritableEvents); readiness != 0 {
  2582  										t.Logf("server.Readiness(%b) = %b but channel not signaled", waiter.WritableEvents, readiness)
  2583  									}
  2584  									continue
  2585  								}
  2586  								break
  2587  							}
  2588  						default:
  2589  							return fmt.Errorf("server.Write failed: %s", err)
  2590  						}
  2591  					}
  2592  					if err := server.Shutdown(tcpip.ShutdownWrite); err != nil {
  2593  						return fmt.Errorf("server.Shutdown failed: %s", err)
  2594  					}
  2595  					t.Logf("server end shutdown done")
  2596  					return nil
  2597  				}(); err != nil {
  2598  					t.Error(err)
  2599  				}
  2600  			}()
  2601  
  2602  			go func() {
  2603  				defer wg.Done()
  2604  
  2605  				if err := func() error {
  2606  					total := 0
  2607  					defer t.Logf("read %d bytes in total", total)
  2608  					for {
  2609  						switch res, err := client.Read(ioutil.Discard, tcpip.ReadOptions{}); err.(type) {
  2610  						case nil:
  2611  							t.Logf("read %d bytes", res.Count)
  2612  							total += res.Count
  2613  							t.Logf("read total %d bytes till now", total)
  2614  						case *tcpip.ErrClosedForReceive:
  2615  							return nil
  2616  						case *tcpip.ErrWouldBlock:
  2617  							for {
  2618  								t.Logf("waiting on client")
  2619  								select {
  2620  								case <-clientCh:
  2621  								case <-time.After(time.Second):
  2622  									if readiness := client.Readiness(waiter.ReadableEvents); readiness != 0 {
  2623  										return fmt.Errorf("client.Readiness(%b) = %b but channel not signaled", waiter.ReadableEvents, readiness)
  2624  									}
  2625  									continue
  2626  								}
  2627  								break
  2628  							}
  2629  						default:
  2630  							return fmt.Errorf("client.Write failed: %s", err)
  2631  						}
  2632  					}
  2633  				}(); err != nil {
  2634  					t.Error(err)
  2635  				}
  2636  			}()
  2637  		})
  2638  	}
  2639  }
  2640  
  2641  // Test the stack receive window advertisement on receiving segments smaller than
  2642  // segment overhead. It tests for the right edge of the window to not grow when
  2643  // the endpoint is not being read from.
  2644  func TestSmallSegReceiveWindowAdvertisement(t *testing.T) {
  2645  	c := context.New(t, e2e.DefaultMTU)
  2646  	defer c.Cleanup()
  2647  
  2648  	opt := tcpip.TCPReceiveBufferSizeRangeOption{
  2649  		Min:     1,
  2650  		Default: tcp.DefaultReceiveBufferSize,
  2651  		Max:     tcp.DefaultReceiveBufferSize << tcp.FindWndScale(seqnum.Size(tcp.DefaultReceiveBufferSize)),
  2652  	}
  2653  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  2654  		t.Fatalf("SetTransportProtocolOption(%d, &%#v): %s", tcp.ProtocolNumber, opt, err)
  2655  	}
  2656  
  2657  	c.AcceptWithOptionsNoDelay(tcp.FindWndScale(seqnum.Size(opt.Default)), header.TCPSynOptions{MSS: e2e.DefaultIPv4MSS})
  2658  
  2659  	// Bump up the receive buffer size such that, when the receive window grows,
  2660  	// the scaled window exceeds maxUint16.
  2661  	c.EP.SocketOptions().SetReceiveBufferSize(int64(opt.Max)*2, true /* notify */)
  2662  
  2663  	// Keep the payload size < segment overhead and such that it is a multiple
  2664  	// of the window scaled value. This enables the test to perform equality
  2665  	// checks on the incoming receive window.
  2666  	payloadSize := 1 << c.RcvdWindowScale
  2667  	if payloadSize >= tcp.SegOverheadSize {
  2668  		t.Fatalf("payload size of %d is not less than the segment overhead of %d", payloadSize, tcp.SegOverheadSize)
  2669  	}
  2670  	payload := generateRandomPayload(t, payloadSize)
  2671  	payloadLen := seqnum.Size(len(payload))
  2672  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  2673  
  2674  	// Send payload to the endpoint and return the advertised receive window
  2675  	// from the endpoint.
  2676  	getIncomingRcvWnd := func() uint32 {
  2677  		c.SendPacket(payload, &context.Headers{
  2678  			SrcPort: context.TestPort,
  2679  			DstPort: c.Port,
  2680  			SeqNum:  iss,
  2681  			AckNum:  c.IRS.Add(1),
  2682  			Flags:   header.TCPFlagAck,
  2683  			RcvWnd:  30000,
  2684  		})
  2685  		iss = iss.Add(payloadLen)
  2686  
  2687  		pkt := c.GetPacket()
  2688  		defer pkt.Release()
  2689  		return uint32(header.TCP(header.IPv4(pkt.AsSlice()).Payload()).WindowSize()) << c.RcvdWindowScale
  2690  	}
  2691  
  2692  	// Read the advertised receive window with the ACK for payload.
  2693  	rcvWnd := getIncomingRcvWnd()
  2694  
  2695  	// Check if the subsequent ACK to our send has not grown the right edge of
  2696  	// the window.
  2697  	if got, want := getIncomingRcvWnd(), rcvWnd-uint32(len(payload)); got != want {
  2698  		t.Fatalf("got incomingRcvwnd %d want %d", got, want)
  2699  	}
  2700  
  2701  	// Read the data so that the subsequent ACK from the endpoint
  2702  	// grows the right edge of the window.
  2703  	var buf bytes.Buffer
  2704  	if _, err := c.EP.Read(&buf, tcpip.ReadOptions{}); err != nil {
  2705  		t.Fatalf("c.EP.Read: %s", err)
  2706  	}
  2707  
  2708  	// Check if we have received max uint16 as our advertised
  2709  	// scaled window now after a read above.
  2710  	maxRcv := uint32(math.MaxUint16 << c.RcvdWindowScale)
  2711  	if got, want := getIncomingRcvWnd(), maxRcv; got != want {
  2712  		t.Fatalf("got incomingRcvwnd %d want %d", got, want)
  2713  	}
  2714  
  2715  	// Check if the subsequent ACK to our send has not grown the right edge of
  2716  	// the window.
  2717  	if got, want := getIncomingRcvWnd(), maxRcv-uint32(len(payload)); got != want {
  2718  		t.Fatalf("got incomingRcvwnd %d want %d", got, want)
  2719  	}
  2720  }
  2721  
  2722  func TestNoWindowShrinking(t *testing.T) {
  2723  	c := context.New(t, e2e.DefaultMTU)
  2724  	defer c.Cleanup()
  2725  
  2726  	// Start off with a certain receive buffer then cut it in half and verify that
  2727  	// the right edge of the window does not shrink.
  2728  	// NOTE: Netstack doubles the value specified here.
  2729  	rcvBufSize := 65536
  2730  	// Enable window scaling with a scale of zero from our end.
  2731  	c.CreateConnectedWithRawOptions(context.TestInitialSequenceNumber, 30000, rcvBufSize, []byte{
  2732  		header.TCPOptionWS, 3, 0, header.TCPOptionNOP,
  2733  	})
  2734  
  2735  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  2736  	c.WQ.EventRegister(&we)
  2737  	defer c.WQ.EventUnregister(&we)
  2738  
  2739  	ept := endpointTester{c.EP}
  2740  	ept.CheckReadError(t, &tcpip.ErrWouldBlock{})
  2741  
  2742  	// Send a 1 byte payload so that we can record the current receive window.
  2743  	// Send a payload of half the size of rcvBufSize.
  2744  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  2745  	payload := []byte{1}
  2746  	c.SendPacket(payload, &context.Headers{
  2747  		SrcPort: context.TestPort,
  2748  		DstPort: c.Port,
  2749  		Flags:   header.TCPFlagAck,
  2750  		SeqNum:  iss,
  2751  		AckNum:  c.IRS.Add(1),
  2752  		RcvWnd:  30000,
  2753  	})
  2754  
  2755  	// Wait for receive to be notified.
  2756  	select {
  2757  	case <-ch:
  2758  	case <-time.After(5 * time.Second):
  2759  		t.Fatalf("Timed out waiting for data to arrive")
  2760  	}
  2761  
  2762  	// Read the 1 byte payload we just sent.
  2763  	if got, want := payload, ept.CheckRead(t); !bytes.Equal(got, want) {
  2764  		t.Fatalf("got data: %v, want: %v", got, want)
  2765  	}
  2766  
  2767  	// Verify that the ACK does not shrink the window.
  2768  	pkt := c.GetPacket()
  2769  	defer pkt.Release()
  2770  	iss = iss.Add(1)
  2771  	checker.IPv4(t, pkt,
  2772  		checker.TCP(
  2773  			checker.DstPort(context.TestPort),
  2774  			checker.TCPSeqNum(uint32(c.IRS)+1),
  2775  			checker.TCPAckNum(uint32(iss)),
  2776  			checker.TCPFlags(header.TCPFlagAck),
  2777  		),
  2778  	)
  2779  	// Stash the initial window.
  2780  	initialWnd := header.TCP(header.IPv4(pkt.AsSlice()).Payload()).WindowSize() << c.RcvdWindowScale
  2781  	initialLastAcceptableSeq := iss.Add(seqnum.Size(initialWnd))
  2782  	// Now shrink the receive buffer to half its original size.
  2783  	c.EP.SocketOptions().SetReceiveBufferSize(int64(rcvBufSize), true /* notify */)
  2784  
  2785  	data := generateRandomPayload(t, rcvBufSize)
  2786  	// Send a payload of half the size of rcvBufSize.
  2787  	c.SendPacket(data[:rcvBufSize/2], &context.Headers{
  2788  		SrcPort: context.TestPort,
  2789  		DstPort: c.Port,
  2790  		Flags:   header.TCPFlagAck,
  2791  		SeqNum:  iss,
  2792  		AckNum:  c.IRS.Add(1),
  2793  		RcvWnd:  30000,
  2794  	})
  2795  	iss = iss.Add(seqnum.Size(rcvBufSize / 2))
  2796  
  2797  	// Verify that the ACK does not shrink the window.
  2798  	pkt = c.GetPacket()
  2799  	defer pkt.Release()
  2800  	checker.IPv4(t, pkt,
  2801  		checker.TCP(
  2802  			checker.DstPort(context.TestPort),
  2803  			checker.TCPSeqNum(uint32(c.IRS)+1),
  2804  			checker.TCPAckNum(uint32(iss)),
  2805  			checker.TCPFlags(header.TCPFlagAck),
  2806  		),
  2807  	)
  2808  	newWnd := header.TCP(header.IPv4(pkt.AsSlice()).Payload()).WindowSize() << c.RcvdWindowScale
  2809  	newLastAcceptableSeq := iss.Add(seqnum.Size(newWnd))
  2810  	if newLastAcceptableSeq.LessThan(initialLastAcceptableSeq) {
  2811  		t.Fatalf("receive window shrunk unexpectedly got: %d, want >= %d", newLastAcceptableSeq, initialLastAcceptableSeq)
  2812  	}
  2813  
  2814  	// Send another payload of half the size of rcvBufSize. This should fill up the
  2815  	// socket receive buffer and we should see a zero window.
  2816  	c.SendPacket(data[rcvBufSize/2:], &context.Headers{
  2817  		SrcPort: context.TestPort,
  2818  		DstPort: c.Port,
  2819  		Flags:   header.TCPFlagAck,
  2820  		SeqNum:  iss,
  2821  		AckNum:  c.IRS.Add(1),
  2822  		RcvWnd:  30000,
  2823  	})
  2824  	iss = iss.Add(seqnum.Size(rcvBufSize / 2))
  2825  
  2826  	v := c.GetPacket()
  2827  	defer v.Release()
  2828  	checker.IPv4(t, v, checker.TCP(
  2829  		checker.DstPort(context.TestPort),
  2830  		checker.TCPSeqNum(uint32(c.IRS)+1),
  2831  		checker.TCPAckNum(uint32(iss)),
  2832  		checker.TCPFlags(header.TCPFlagAck),
  2833  		checker.TCPWindow(0),
  2834  	),
  2835  	)
  2836  
  2837  	// Receive data and check it.
  2838  	read := ept.CheckReadFull(t, len(data), ch, 5*time.Second)
  2839  	if !bytes.Equal(data, read) {
  2840  		t.Fatalf("got data = %v, want = %v", read, data)
  2841  	}
  2842  
  2843  	// Check that we get an ACK for the newly non-zero window, which is the new
  2844  	// receive buffer size we set after the connection was established.
  2845  	v = c.GetPacket()
  2846  	defer v.Release()
  2847  	checker.IPv4(t, v, checker.TCP(
  2848  		checker.DstPort(context.TestPort),
  2849  		checker.TCPSeqNum(uint32(c.IRS)+1),
  2850  		checker.TCPAckNum(uint32(iss)),
  2851  		checker.TCPFlags(header.TCPFlagAck),
  2852  		checker.TCPWindow(uint16(rcvBufSize/2)>>c.RcvdWindowScale),
  2853  	),
  2854  	)
  2855  }
  2856  
  2857  func TestSimpleSend(t *testing.T) {
  2858  	c := context.New(t, e2e.DefaultMTU)
  2859  	defer c.Cleanup()
  2860  
  2861  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  2862  
  2863  	data := []byte{1, 2, 3}
  2864  	var r bytes.Reader
  2865  	r.Reset(data)
  2866  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  2867  		t.Fatalf("Write failed: %s", err)
  2868  	}
  2869  
  2870  	// Check that data is received.
  2871  	b := c.GetPacket()
  2872  	defer b.Release()
  2873  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  2874  	checker.IPv4(t, b,
  2875  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  2876  		checker.TCP(
  2877  			checker.DstPort(context.TestPort),
  2878  			checker.TCPSeqNum(uint32(c.IRS)+1),
  2879  			checker.TCPAckNum(uint32(iss)),
  2880  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  2881  		),
  2882  	)
  2883  
  2884  	if p := b.AsSlice()[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(data, p) {
  2885  		t.Fatalf("got data = %v, want = %v", p, data)
  2886  	}
  2887  
  2888  	// Acknowledge the data.
  2889  	c.SendPacket(nil, &context.Headers{
  2890  		SrcPort: context.TestPort,
  2891  		DstPort: c.Port,
  2892  		Flags:   header.TCPFlagAck,
  2893  		SeqNum:  iss,
  2894  		AckNum:  c.IRS.Add(1 + seqnum.Size(len(data))),
  2895  		RcvWnd:  30000,
  2896  	})
  2897  }
  2898  
  2899  func TestZeroWindowSend(t *testing.T) {
  2900  	c := context.New(t, e2e.DefaultMTU)
  2901  	defer c.Cleanup()
  2902  
  2903  	c.CreateConnected(context.TestInitialSequenceNumber, 0 /* rcvWnd */, -1 /* epRcvBuf */)
  2904  
  2905  	data := []byte{1, 2, 3}
  2906  	var r bytes.Reader
  2907  	r.Reset(data)
  2908  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  2909  		t.Fatalf("Write failed: %s", err)
  2910  	}
  2911  
  2912  	// Check if we got a zero-window probe.
  2913  	b := c.GetPacket()
  2914  	defer b.Release()
  2915  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  2916  	checker.IPv4(t, b,
  2917  		checker.PayloadLen(header.TCPMinimumSize+1),
  2918  		checker.TCP(
  2919  			checker.DstPort(context.TestPort),
  2920  			checker.TCPSeqNum(uint32(c.IRS)),
  2921  			checker.TCPAckNum(uint32(iss)),
  2922  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  2923  		),
  2924  	)
  2925  
  2926  	// Open up the window. Data should be received now.
  2927  	c.SendPacket(nil, &context.Headers{
  2928  		SrcPort: context.TestPort,
  2929  		DstPort: c.Port,
  2930  		Flags:   header.TCPFlagAck,
  2931  		SeqNum:  iss,
  2932  		AckNum:  c.IRS.Add(1),
  2933  		RcvWnd:  30000,
  2934  	})
  2935  
  2936  	// Check that data is received.
  2937  	b = c.GetPacket()
  2938  	defer b.Release()
  2939  	checker.IPv4(t, b,
  2940  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  2941  		checker.TCP(
  2942  			checker.DstPort(context.TestPort),
  2943  			checker.TCPSeqNum(uint32(c.IRS)+1),
  2944  			checker.TCPAckNum(uint32(iss)),
  2945  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  2946  		),
  2947  	)
  2948  
  2949  	if p := b.AsSlice()[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(data, p) {
  2950  		t.Fatalf("got data = %v, want = %v", p, data)
  2951  	}
  2952  
  2953  	// Acknowledge the data.
  2954  	c.SendPacket(nil, &context.Headers{
  2955  		SrcPort: context.TestPort,
  2956  		DstPort: c.Port,
  2957  		Flags:   header.TCPFlagAck,
  2958  		SeqNum:  iss,
  2959  		AckNum:  c.IRS.Add(1 + seqnum.Size(len(data))),
  2960  		RcvWnd:  30000,
  2961  	})
  2962  }
  2963  
  2964  func TestScaledWindowConnect(t *testing.T) {
  2965  	// This test ensures that window scaling is used when the peer
  2966  	// does advertise it and connection is established with Connect().
  2967  	c := context.New(t, e2e.DefaultMTU)
  2968  	defer c.Cleanup()
  2969  
  2970  	// Set the window size greater than the maximum non-scaled window.
  2971  	c.CreateConnectedWithRawOptions(context.TestInitialSequenceNumber, 30000, 65535*3, []byte{
  2972  		header.TCPOptionWS, 3, 0, header.TCPOptionNOP,
  2973  	})
  2974  
  2975  	data := []byte{1, 2, 3}
  2976  	var r bytes.Reader
  2977  	r.Reset(data)
  2978  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  2979  		t.Fatalf("Write failed: %s", err)
  2980  	}
  2981  
  2982  	// Check that data is received, and that advertised window is 0x5fff,
  2983  	// that is, that it is scaled.
  2984  	b := c.GetPacket()
  2985  	defer b.Release()
  2986  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  2987  	checker.IPv4(t, b,
  2988  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  2989  		checker.TCP(
  2990  			checker.DstPort(context.TestPort),
  2991  			checker.TCPSeqNum(uint32(c.IRS)+1),
  2992  			checker.TCPAckNum(uint32(iss)),
  2993  			checker.TCPWindow(0x5fff),
  2994  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  2995  		),
  2996  	)
  2997  }
  2998  
  2999  func TestNonScaledWindowConnect(t *testing.T) {
  3000  	// This test ensures that window scaling is not used when the peer
  3001  	// doesn't advertise it and connection is established with Connect().
  3002  	c := context.New(t, e2e.DefaultMTU)
  3003  	defer c.Cleanup()
  3004  
  3005  	// Set the window size greater than the maximum non-scaled window.
  3006  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, 65535*3)
  3007  
  3008  	data := []byte{1, 2, 3}
  3009  	var r bytes.Reader
  3010  	r.Reset(data)
  3011  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  3012  		t.Fatalf("Write failed: %s", err)
  3013  	}
  3014  
  3015  	// Check that data is received, and that advertised window is 0xffff,
  3016  	// that is, that it's not scaled.
  3017  	b := c.GetPacket()
  3018  	defer b.Release()
  3019  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  3020  	checker.IPv4(t, b,
  3021  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  3022  		checker.TCP(
  3023  			checker.DstPort(context.TestPort),
  3024  			checker.TCPSeqNum(uint32(c.IRS)+1),
  3025  			checker.TCPAckNum(uint32(iss)),
  3026  			checker.TCPWindow(0xffff),
  3027  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  3028  		),
  3029  	)
  3030  }
  3031  
  3032  func TestScaledWindowAccept(t *testing.T) {
  3033  	// This test ensures that window scaling is used when the peer
  3034  	// does advertise it and connection is established with Accept().
  3035  	c := context.New(t, e2e.DefaultMTU)
  3036  	defer c.Cleanup()
  3037  
  3038  	// Create EP and start listening.
  3039  	wq := &waiter.Queue{}
  3040  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  3041  	if err != nil {
  3042  		t.Fatalf("NewEndpoint failed: %s", err)
  3043  	}
  3044  	defer ep.Close()
  3045  
  3046  	// Set the window size greater than the maximum non-scaled window.
  3047  	ep.SocketOptions().SetReceiveBufferSize(65535*6, true /* notify */)
  3048  
  3049  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  3050  		t.Fatalf("Bind failed: %s", err)
  3051  	}
  3052  
  3053  	if err := ep.Listen(10); err != nil {
  3054  		t.Fatalf("Listen failed: %s", err)
  3055  	}
  3056  
  3057  	// Do 3-way handshake.
  3058  	// wndScale expected is 3 as 65535 * 3 * 2 < 65535 * 2^3 but > 65535 *2 *2
  3059  	c.PassiveConnectWithOptions(100, 3 /* wndScale */, header.TCPSynOptions{MSS: e2e.DefaultIPv4MSS}, 0 /* delay */)
  3060  
  3061  	// Try to accept the connection.
  3062  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  3063  	wq.EventRegister(&we)
  3064  	defer wq.EventUnregister(&we)
  3065  
  3066  	c.EP, _, err = ep.Accept(nil)
  3067  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  3068  		// Wait for connection to be established.
  3069  		select {
  3070  		case <-ch:
  3071  			c.EP, _, err = ep.Accept(nil)
  3072  			if err != nil {
  3073  				t.Fatalf("Accept failed: %s", err)
  3074  			}
  3075  
  3076  		case <-time.After(1 * time.Second):
  3077  			t.Fatalf("Timed out waiting for accept")
  3078  		}
  3079  	}
  3080  
  3081  	data := []byte{1, 2, 3}
  3082  	var r bytes.Reader
  3083  	r.Reset(data)
  3084  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  3085  		t.Fatalf("Write failed: %s", err)
  3086  	}
  3087  
  3088  	// Check that data is received, and that advertised window is 0x5fff,
  3089  	// that is, that it is scaled.
  3090  	b := c.GetPacket()
  3091  	defer b.Release()
  3092  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  3093  	checker.IPv4(t, b,
  3094  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  3095  		checker.TCP(
  3096  			checker.DstPort(context.TestPort),
  3097  			checker.TCPSeqNum(uint32(c.IRS)+1),
  3098  			checker.TCPAckNum(uint32(iss)),
  3099  			checker.TCPWindow(0x5fff),
  3100  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  3101  		),
  3102  	)
  3103  }
  3104  
  3105  func TestNonScaledWindowAccept(t *testing.T) {
  3106  	// This test ensures that window scaling is not used when the peer
  3107  	// doesn't advertise it and connection is established with Accept().
  3108  	c := context.New(t, e2e.DefaultMTU)
  3109  	defer c.Cleanup()
  3110  
  3111  	// Create EP and start listening.
  3112  	wq := &waiter.Queue{}
  3113  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  3114  	if err != nil {
  3115  		t.Fatalf("NewEndpoint failed: %s", err)
  3116  	}
  3117  	defer ep.Close()
  3118  
  3119  	// Set the window size greater than the maximum non-scaled window.
  3120  	ep.SocketOptions().SetReceiveBufferSize(65535*6, true /* notify */)
  3121  
  3122  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  3123  		t.Fatalf("Bind failed: %s", err)
  3124  	}
  3125  
  3126  	if err := ep.Listen(10); err != nil {
  3127  		t.Fatalf("Listen failed: %s", err)
  3128  	}
  3129  
  3130  	// Do 3-way handshake w/ window scaling disabled. The SYN-ACK to the SYN
  3131  	// should not carry the window scaling option.
  3132  	c.PassiveConnect(100, -1, header.TCPSynOptions{MSS: e2e.DefaultIPv4MSS})
  3133  
  3134  	// Try to accept the connection.
  3135  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  3136  	wq.EventRegister(&we)
  3137  	defer wq.EventUnregister(&we)
  3138  
  3139  	c.EP, _, err = ep.Accept(nil)
  3140  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  3141  		// Wait for connection to be established.
  3142  		select {
  3143  		case <-ch:
  3144  			c.EP, _, err = ep.Accept(nil)
  3145  			if err != nil {
  3146  				t.Fatalf("Accept failed: %s", err)
  3147  			}
  3148  
  3149  		case <-time.After(1 * time.Second):
  3150  			t.Fatalf("Timed out waiting for accept")
  3151  		}
  3152  	}
  3153  
  3154  	data := []byte{1, 2, 3}
  3155  	var r bytes.Reader
  3156  	r.Reset(data)
  3157  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  3158  		t.Fatalf("Write failed: %s", err)
  3159  	}
  3160  
  3161  	// Check that data is received, and that advertised window is 0xffff,
  3162  	// that is, that it's not scaled.
  3163  	b := c.GetPacket()
  3164  	defer b.Release()
  3165  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  3166  	checker.IPv4(t, b,
  3167  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  3168  		checker.TCP(
  3169  			checker.DstPort(context.TestPort),
  3170  			checker.TCPSeqNum(uint32(c.IRS)+1),
  3171  			checker.TCPAckNum(uint32(iss)),
  3172  			checker.TCPWindow(0xffff),
  3173  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  3174  		),
  3175  	)
  3176  }
  3177  
  3178  func TestZeroScaledWindowReceive(t *testing.T) {
  3179  	// This test ensures that the endpoint sends a non-zero window size
  3180  	// advertisement when the scaled window transitions from 0 to non-zero,
  3181  	// but the actual window (not scaled) hasn't gotten to zero.
  3182  	c := context.New(t, e2e.DefaultMTU)
  3183  	defer c.Cleanup()
  3184  
  3185  	// Set the buffer size such that a window scale of 5 will be used.
  3186  	const bufSz = 65535 * 10
  3187  	const ws = uint32(5)
  3188  	c.CreateConnectedWithRawOptions(context.TestInitialSequenceNumber, 30000, bufSz, []byte{
  3189  		header.TCPOptionWS, 3, 0, header.TCPOptionNOP,
  3190  	})
  3191  
  3192  	// Write chunks of 50000 bytes.
  3193  	remain := 0
  3194  	sent := 0
  3195  	data := make([]byte, 50000)
  3196  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  3197  	// Keep writing till the window drops below len(data).
  3198  	for {
  3199  		c.SendPacket(data, &context.Headers{
  3200  			SrcPort: context.TestPort,
  3201  			DstPort: c.Port,
  3202  			Flags:   header.TCPFlagAck,
  3203  			SeqNum:  iss.Add(seqnum.Size(sent)),
  3204  			AckNum:  c.IRS.Add(1),
  3205  			RcvWnd:  30000,
  3206  		})
  3207  		sent += len(data)
  3208  		pkt := c.GetPacket()
  3209  		defer pkt.Release()
  3210  		checker.IPv4(t, pkt,
  3211  			checker.PayloadLen(header.TCPMinimumSize),
  3212  			checker.TCP(
  3213  				checker.DstPort(context.TestPort),
  3214  				checker.TCPSeqNum(uint32(c.IRS)+1),
  3215  				checker.TCPAckNum(uint32(iss)+uint32(sent)),
  3216  				checker.TCPFlags(header.TCPFlagAck),
  3217  			),
  3218  		)
  3219  		// Don't reduce window to zero here.
  3220  		if wnd := int(header.TCP(header.IPv4(pkt.AsSlice()).Payload()).WindowSize()); wnd<<ws < len(data) {
  3221  			remain = wnd << ws
  3222  			break
  3223  		}
  3224  	}
  3225  
  3226  	// Make the window non-zero, but the scaled window zero.
  3227  	for remain >= 16 {
  3228  		data = data[:remain-15]
  3229  		c.SendPacket(data, &context.Headers{
  3230  			SrcPort: context.TestPort,
  3231  			DstPort: c.Port,
  3232  			Flags:   header.TCPFlagAck,
  3233  			SeqNum:  iss.Add(seqnum.Size(sent)),
  3234  			AckNum:  c.IRS.Add(1),
  3235  			RcvWnd:  30000,
  3236  		})
  3237  		sent += len(data)
  3238  		pkt := c.GetPacket()
  3239  		defer pkt.Release()
  3240  		checker.IPv4(t, pkt,
  3241  			checker.PayloadLen(header.TCPMinimumSize),
  3242  			checker.TCP(
  3243  				checker.DstPort(context.TestPort),
  3244  				checker.TCPSeqNum(uint32(c.IRS)+1),
  3245  				checker.TCPAckNum(uint32(iss)+uint32(sent)),
  3246  				checker.TCPFlags(header.TCPFlagAck),
  3247  			),
  3248  		)
  3249  		// Since the receive buffer is split between window advertisement and
  3250  		// application data buffer the window does not always reflect the space
  3251  		// available and actual space available can be a bit more than what is
  3252  		// advertised in the window.
  3253  		wnd := int(header.TCP(header.IPv4(pkt.AsSlice()).Payload()).WindowSize())
  3254  		if wnd == 0 {
  3255  			break
  3256  		}
  3257  		remain = wnd << ws
  3258  	}
  3259  
  3260  	// Read at least 2MSS of data. An ack should be sent in response to that.
  3261  	// Since buffer space is now split in half between window and application
  3262  	// data we need to read more than 1 MSS(65536) of data for a non-zero window
  3263  	// update to be sent. For 1MSS worth of window to be available we need to
  3264  	// read at least 128KB. Since our segments above were 50KB each it means
  3265  	// we need to read at 3 packets.
  3266  	w := tcpip.LimitedWriter{
  3267  		W: ioutil.Discard,
  3268  		N: e2e.DefaultMTU * 2,
  3269  	}
  3270  	for w.N != 0 {
  3271  		res, err := c.EP.Read(&w, tcpip.ReadOptions{})
  3272  		t.Logf("err=%v res=%#v", err, res)
  3273  		if err != nil {
  3274  			t.Fatalf("Read failed: %s", err)
  3275  		}
  3276  	}
  3277  
  3278  	v := c.GetPacket()
  3279  	defer v.Release()
  3280  	checker.IPv4(t, v,
  3281  		checker.PayloadLen(header.TCPMinimumSize),
  3282  		checker.TCP(
  3283  			checker.DstPort(context.TestPort),
  3284  			checker.TCPSeqNum(uint32(c.IRS)+1),
  3285  			checker.TCPAckNum(uint32(iss)+uint32(sent)),
  3286  			checker.TCPWindowGreaterThanEq(uint16(e2e.DefaultMTU>>ws)),
  3287  			checker.TCPFlags(header.TCPFlagAck),
  3288  		))
  3289  }
  3290  
  3291  func TestSegmentMerging(t *testing.T) {
  3292  	tests := []struct {
  3293  		name   string
  3294  		stop   func(tcpip.Endpoint)
  3295  		resume func(tcpip.Endpoint)
  3296  	}{
  3297  		{
  3298  			"stop work",
  3299  			func(ep tcpip.Endpoint) {
  3300  				ep.(interface{ StopWork() }).StopWork()
  3301  			},
  3302  			func(ep tcpip.Endpoint) {
  3303  				ep.(interface{ ResumeWork() }).ResumeWork()
  3304  			},
  3305  		},
  3306  		{
  3307  			"cork",
  3308  			func(ep tcpip.Endpoint) {
  3309  				ep.SocketOptions().SetCorkOption(true)
  3310  			},
  3311  			func(ep tcpip.Endpoint) {
  3312  				ep.SocketOptions().SetCorkOption(false)
  3313  			},
  3314  		},
  3315  	}
  3316  
  3317  	for _, test := range tests {
  3318  		t.Run(test.name, func(t *testing.T) {
  3319  			c := context.New(t, e2e.DefaultMTU)
  3320  			defer c.Cleanup()
  3321  
  3322  			c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  3323  
  3324  			// Send tcp.InitialCwnd number of segments to fill up
  3325  			// InitialWindow but don't ACK. That should prevent
  3326  			// anymore packets from going out.
  3327  			var r bytes.Reader
  3328  			for i := 0; i < tcp.InitialCwnd; i++ {
  3329  				r.Reset([]byte{0})
  3330  				if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  3331  					t.Fatalf("Write #%d failed: %s", i+1, err)
  3332  				}
  3333  			}
  3334  
  3335  			// Now send the segments that should get merged as the congestion
  3336  			// window is full and we won't be able to send any more packets.
  3337  			var allData []byte
  3338  			for i, data := range [][]byte{{1, 2, 3, 4}, {5, 6, 7}, {8, 9}, {10}, {11}} {
  3339  				allData = append(allData, data...)
  3340  				r.Reset(data)
  3341  				if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  3342  					t.Fatalf("Write #%d failed: %s", i+1, err)
  3343  				}
  3344  			}
  3345  
  3346  			// Check that we get tcp.InitialCwnd packets.
  3347  			iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  3348  			for i := 0; i < tcp.InitialCwnd; i++ {
  3349  				b := c.GetPacket()
  3350  				defer b.Release()
  3351  				checker.IPv4(t, b,
  3352  					checker.PayloadLen(header.TCPMinimumSize+1),
  3353  					checker.TCP(
  3354  						checker.DstPort(context.TestPort),
  3355  						checker.TCPSeqNum(uint32(c.IRS)+uint32(i)+1),
  3356  						checker.TCPAckNum(uint32(iss)),
  3357  						checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  3358  					),
  3359  				)
  3360  			}
  3361  
  3362  			// Acknowledge the data.
  3363  			c.SendPacket(nil, &context.Headers{
  3364  				SrcPort: context.TestPort,
  3365  				DstPort: c.Port,
  3366  				Flags:   header.TCPFlagAck,
  3367  				SeqNum:  iss,
  3368  				AckNum:  c.IRS.Add(1 + 10), // 10 for the 10 bytes of payload.
  3369  				RcvWnd:  30000,
  3370  			})
  3371  
  3372  			// Check that data is received.
  3373  			b := c.GetPacket()
  3374  			defer b.Release()
  3375  			checker.IPv4(t, b,
  3376  				checker.PayloadLen(len(allData)+header.TCPMinimumSize),
  3377  				checker.TCP(
  3378  					checker.DstPort(context.TestPort),
  3379  					checker.TCPSeqNum(uint32(c.IRS)+11),
  3380  					checker.TCPAckNum(uint32(iss)),
  3381  					checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  3382  				),
  3383  			)
  3384  
  3385  			if got := b.AsSlice()[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(got, allData) {
  3386  				t.Fatalf("got data = %v, want = %v", got, allData)
  3387  			}
  3388  
  3389  			// Acknowledge the data.
  3390  			c.SendPacket(nil, &context.Headers{
  3391  				SrcPort: context.TestPort,
  3392  				DstPort: c.Port,
  3393  				Flags:   header.TCPFlagAck,
  3394  				SeqNum:  iss,
  3395  				AckNum:  c.IRS.Add(11 + seqnum.Size(len(allData))),
  3396  				RcvWnd:  30000,
  3397  			})
  3398  		})
  3399  	}
  3400  }
  3401  
  3402  func TestDelay(t *testing.T) {
  3403  	c := context.New(t, e2e.DefaultMTU)
  3404  	defer c.Cleanup()
  3405  
  3406  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  3407  
  3408  	c.EP.SocketOptions().SetDelayOption(true)
  3409  
  3410  	var allData []byte
  3411  	for i, data := range [][]byte{{0}, {1, 2, 3, 4}, {5, 6, 7}, {8, 9}, {10}, {11}} {
  3412  		allData = append(allData, data...)
  3413  		var r bytes.Reader
  3414  		r.Reset(data)
  3415  		if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  3416  			t.Fatalf("Write #%d failed: %s", i+1, err)
  3417  		}
  3418  	}
  3419  
  3420  	seq := c.IRS.Add(1)
  3421  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  3422  	for _, want := range [][]byte{allData[:1], allData[1:]} {
  3423  		// Check that data is received.
  3424  		b := c.GetPacket()
  3425  		defer b.Release()
  3426  		checker.IPv4(t, b,
  3427  			checker.PayloadLen(len(want)+header.TCPMinimumSize),
  3428  			checker.TCP(
  3429  				checker.DstPort(context.TestPort),
  3430  				checker.TCPSeqNum(uint32(seq)),
  3431  				checker.TCPAckNum(uint32(iss)),
  3432  				checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  3433  			),
  3434  		)
  3435  
  3436  		if got := b.AsSlice()[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(got, want) {
  3437  			t.Fatalf("got data = %v, want = %v", got, want)
  3438  		}
  3439  
  3440  		seq = seq.Add(seqnum.Size(len(want)))
  3441  		// Acknowledge the data.
  3442  		c.SendPacket(nil, &context.Headers{
  3443  			SrcPort: context.TestPort,
  3444  			DstPort: c.Port,
  3445  			Flags:   header.TCPFlagAck,
  3446  			SeqNum:  iss,
  3447  			AckNum:  seq,
  3448  			RcvWnd:  30000,
  3449  		})
  3450  	}
  3451  }
  3452  
  3453  func TestUndelay(t *testing.T) {
  3454  	c := context.New(t, e2e.DefaultMTU)
  3455  	defer c.Cleanup()
  3456  
  3457  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  3458  
  3459  	c.EP.SocketOptions().SetDelayOption(true)
  3460  
  3461  	allData := [][]byte{{0}, {1, 2, 3}}
  3462  	for i, data := range allData {
  3463  		var r bytes.Reader
  3464  		r.Reset(data)
  3465  		if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  3466  			t.Fatalf("Write #%d failed: %s", i+1, err)
  3467  		}
  3468  	}
  3469  
  3470  	seq := c.IRS.Add(1)
  3471  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  3472  	// Check that data is received.
  3473  	first := c.GetPacket()
  3474  	defer first.Release()
  3475  	checker.IPv4(t, first,
  3476  		checker.PayloadLen(len(allData[0])+header.TCPMinimumSize),
  3477  		checker.TCP(
  3478  			checker.DstPort(context.TestPort),
  3479  			checker.TCPSeqNum(uint32(seq)),
  3480  			checker.TCPAckNum(uint32(iss)),
  3481  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  3482  		),
  3483  	)
  3484  
  3485  	if got, want := first.AsSlice()[header.IPv4MinimumSize+header.TCPMinimumSize:], allData[0]; !bytes.Equal(got, want) {
  3486  		t.Fatalf("got first packet's data = %v, want = %v", got, want)
  3487  	}
  3488  
  3489  	seq = seq.Add(seqnum.Size(len(allData[0])))
  3490  
  3491  	// Check that we don't get the second packet yet.
  3492  	c.CheckNoPacketTimeout("delayed second packet transmitted", 100*time.Millisecond)
  3493  
  3494  	c.EP.SocketOptions().SetDelayOption(false)
  3495  
  3496  	// Check that data is received.
  3497  	second := c.GetPacket()
  3498  	defer second.Release()
  3499  	checker.IPv4(t, second,
  3500  		checker.PayloadLen(len(allData[1])+header.TCPMinimumSize),
  3501  		checker.TCP(
  3502  			checker.DstPort(context.TestPort),
  3503  			checker.TCPSeqNum(uint32(seq)),
  3504  			checker.TCPAckNum(uint32(iss)),
  3505  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  3506  		),
  3507  	)
  3508  
  3509  	if got, want := second.AsSlice()[header.IPv4MinimumSize+header.TCPMinimumSize:], allData[1]; !bytes.Equal(got, want) {
  3510  		t.Fatalf("got second packet's data = %v, want = %v", got, want)
  3511  	}
  3512  
  3513  	seq = seq.Add(seqnum.Size(len(allData[1])))
  3514  
  3515  	// Acknowledge the data.
  3516  	c.SendPacket(nil, &context.Headers{
  3517  		SrcPort: context.TestPort,
  3518  		DstPort: c.Port,
  3519  		Flags:   header.TCPFlagAck,
  3520  		SeqNum:  iss,
  3521  		AckNum:  seq,
  3522  		RcvWnd:  30000,
  3523  	})
  3524  }
  3525  
  3526  func TestMSSNotDelayed(t *testing.T) {
  3527  	tests := []struct {
  3528  		name string
  3529  		fn   func(tcpip.Endpoint)
  3530  	}{
  3531  		{"no-op", func(tcpip.Endpoint) {}},
  3532  		{"delay", func(ep tcpip.Endpoint) { ep.SocketOptions().SetDelayOption(true) }},
  3533  		{"cork", func(ep tcpip.Endpoint) { ep.SocketOptions().SetCorkOption(true) }},
  3534  	}
  3535  
  3536  	for _, test := range tests {
  3537  		t.Run(test.name, func(t *testing.T) {
  3538  			const maxPayload = 100
  3539  			c := context.New(t, e2e.DefaultMTU)
  3540  			defer c.Cleanup()
  3541  
  3542  			c.CreateConnectedWithRawOptions(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */, []byte{
  3543  				header.TCPOptionMSS, 4, byte(maxPayload / 256), byte(maxPayload % 256),
  3544  			})
  3545  
  3546  			test.fn(c.EP)
  3547  
  3548  			allData := [][]byte{{0}, make([]byte, maxPayload), make([]byte, maxPayload)}
  3549  			for i, data := range allData {
  3550  				var r bytes.Reader
  3551  				r.Reset(data)
  3552  				if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  3553  					t.Fatalf("Write #%d failed: %s", i+1, err)
  3554  				}
  3555  			}
  3556  
  3557  			seq := c.IRS.Add(1)
  3558  			iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  3559  			for i, data := range allData {
  3560  				// Check that data is received.
  3561  				packet := c.GetPacket()
  3562  				defer packet.Release()
  3563  				checker.IPv4(t, packet,
  3564  					checker.PayloadLen(len(data)+header.TCPMinimumSize),
  3565  					checker.TCP(
  3566  						checker.DstPort(context.TestPort),
  3567  						checker.TCPSeqNum(uint32(seq)),
  3568  						checker.TCPAckNum(uint32(iss)),
  3569  						checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  3570  					),
  3571  				)
  3572  
  3573  				if got, want := packet.AsSlice()[header.IPv4MinimumSize+header.TCPMinimumSize:], data; !bytes.Equal(got, want) {
  3574  					t.Fatalf("got packet #%d's data = %v, want = %v", i+1, got, want)
  3575  				}
  3576  
  3577  				seq = seq.Add(seqnum.Size(len(data)))
  3578  			}
  3579  
  3580  			// Acknowledge the data.
  3581  			c.SendPacket(nil, &context.Headers{
  3582  				SrcPort: context.TestPort,
  3583  				DstPort: c.Port,
  3584  				Flags:   header.TCPFlagAck,
  3585  				SeqNum:  iss,
  3586  				AckNum:  seq,
  3587  				RcvWnd:  30000,
  3588  			})
  3589  		})
  3590  	}
  3591  }
  3592  
  3593  func TestSendGreaterThanMTU(t *testing.T) {
  3594  	const maxPayload = 100
  3595  	c := context.New(t, uint32(header.TCPMinimumSize+header.IPv4MinimumSize+maxPayload))
  3596  	defer c.Cleanup()
  3597  
  3598  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  3599  	e2e.CheckBrokenUpWrite(t, c, maxPayload)
  3600  }
  3601  
  3602  func TestDefaultTTL(t *testing.T) {
  3603  	for _, test := range []struct {
  3604  		name     string
  3605  		protoNum tcpip.NetworkProtocolNumber
  3606  		addr     tcpip.Address
  3607  	}{
  3608  		{"ipv4", ipv4.ProtocolNumber, context.TestAddr},
  3609  		{"ipv6", ipv6.ProtocolNumber, context.TestV6Addr},
  3610  	} {
  3611  		t.Run(test.name, func(t *testing.T) {
  3612  			c := context.New(t, 65535)
  3613  			defer c.Cleanup()
  3614  
  3615  			var err tcpip.Error
  3616  			c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, test.protoNum, &waiter.Queue{})
  3617  			if err != nil {
  3618  				t.Fatalf("NewEndpoint failed: %s", err)
  3619  			}
  3620  
  3621  			proto := c.Stack().NetworkProtocolInstance(test.protoNum)
  3622  			if proto == nil {
  3623  				t.Fatalf("c.s.NetworkProtocolInstance(flow.netProto()) did not return a protocol")
  3624  			}
  3625  
  3626  			var initialDefaultTTL tcpip.DefaultTTLOption
  3627  			if err := proto.Option(&initialDefaultTTL); err != nil {
  3628  				t.Fatalf("proto.Option(&initialDefaultTTL) (%T) failed: %s", initialDefaultTTL, err)
  3629  			}
  3630  
  3631  			{
  3632  				err := c.EP.Connect(tcpip.FullAddress{Addr: test.addr, Port: context.TestPort})
  3633  				if d := cmp.Diff(&tcpip.ErrConnectStarted{}, err); d != "" {
  3634  					t.Fatalf("c.EP.Connect(...) mismatch (-want +got):\n%s", d)
  3635  				}
  3636  			}
  3637  
  3638  			checkTTL := func(ttl uint8) {
  3639  				if test.protoNum == ipv4.ProtocolNumber {
  3640  					v := c.GetPacket()
  3641  					defer v.Release()
  3642  					checker.IPv4(t, v, checker.TTL(ttl))
  3643  				} else {
  3644  					v := c.GetV6Packet()
  3645  					defer v.Release()
  3646  					checker.IPv6(t, v, checker.TTL(ttl))
  3647  				}
  3648  			}
  3649  
  3650  			// Receive SYN packet.
  3651  			checkTTL(uint8(initialDefaultTTL))
  3652  
  3653  			newDefaultTTL := tcpip.DefaultTTLOption(initialDefaultTTL + 1)
  3654  			if err := proto.SetOption(&newDefaultTTL); err != nil {
  3655  				t.Fatalf("proto.SetOption(&%T(%d))) failed: %s", newDefaultTTL, newDefaultTTL, err)
  3656  			}
  3657  
  3658  			// Receive retransmitted SYN packet.
  3659  			checkTTL(uint8(newDefaultTTL))
  3660  		})
  3661  	}
  3662  }
  3663  
  3664  func TestSetTTL(t *testing.T) {
  3665  	for _, test := range []struct {
  3666  		name          string
  3667  		protoNum      tcpip.NetworkProtocolNumber
  3668  		addr          tcpip.Address
  3669  		relevantOpt   tcpip.SockOptInt
  3670  		irrelevantOpt tcpip.SockOptInt
  3671  	}{
  3672  		{"ipv4", ipv4.ProtocolNumber, context.TestAddr, tcpip.IPv4TTLOption, tcpip.IPv6HopLimitOption},
  3673  		{"ipv6", ipv6.ProtocolNumber, context.TestV6Addr, tcpip.IPv6HopLimitOption, tcpip.IPv4TTLOption},
  3674  	} {
  3675  		t.Run(test.name, func(t *testing.T) {
  3676  			for _, wantTTL := range []uint8{1, 2, 50, 64, 128, 254, 255} {
  3677  				t.Run(fmt.Sprintf("TTL:%d", wantTTL), func(t *testing.T) {
  3678  					c := context.New(t, 65535)
  3679  					defer c.Cleanup()
  3680  
  3681  					var err tcpip.Error
  3682  					c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, test.protoNum, &waiter.Queue{})
  3683  					if err != nil {
  3684  						t.Fatalf("NewEndpoint failed: %s", err)
  3685  					}
  3686  
  3687  					if err := c.EP.SetSockOptInt(test.relevantOpt, int(wantTTL)); err != nil {
  3688  						t.Fatalf("SetSockOptInt(%d, %d) failed: %s", test.relevantOpt, wantTTL, err)
  3689  					}
  3690  					// Set a different ttl/hoplimit for the unused protocol, showing that
  3691  					// it does not affect the other protocol.
  3692  					if err := c.EP.SetSockOptInt(test.irrelevantOpt, int(wantTTL+1)); err != nil {
  3693  						t.Fatalf("SetSockOptInt(%d, %d) failed: %s", test.irrelevantOpt, wantTTL, err)
  3694  					}
  3695  
  3696  					{
  3697  						err := c.EP.Connect(tcpip.FullAddress{Addr: test.addr, Port: context.TestPort})
  3698  						if d := cmp.Diff(&tcpip.ErrConnectStarted{}, err); d != "" {
  3699  							t.Fatalf("c.EP.Connect(...) mismatch (-want +got):\n%s", d)
  3700  						}
  3701  					}
  3702  
  3703  					// Receive SYN packet.
  3704  					if test.protoNum == ipv4.ProtocolNumber {
  3705  						v := c.GetPacket()
  3706  						defer v.Release()
  3707  						checker.IPv4(t, v, checker.TTL(wantTTL))
  3708  					} else {
  3709  						v := c.GetV6Packet()
  3710  						defer v.Release()
  3711  						checker.IPv6(t, v, checker.TTL(wantTTL))
  3712  					}
  3713  				})
  3714  			}
  3715  		})
  3716  	}
  3717  }
  3718  
  3719  func TestSendMSSLessThanOptionsSize(t *testing.T) {
  3720  	const mss = 10
  3721  	const writeSize = 300
  3722  	c := context.New(t, 65535)
  3723  	defer c.Cleanup()
  3724  
  3725  	// The sizes of these options add up to 12.
  3726  	c.CreateConnectedWithRawOptions(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */, []byte{
  3727  		header.TCPOptionMSS, 4, byte(mss / 256), byte(mss % 256),
  3728  		header.TCPOptionTS, header.TCPOptionTSLength, 1, 2, 3, 4, 5, 6, 7, 8,
  3729  		header.TCPOptionSACKPermitted, header.TCPOptionSackPermittedLength,
  3730  	})
  3731  	e2e.CheckBrokenUpWrite(t, c, writeSize)
  3732  
  3733  	var r bytes.Reader
  3734  	r.Reset(make([]byte, writeSize))
  3735  	_, err := c.EP.Write(&r, tcpip.WriteOptions{})
  3736  	if err != nil {
  3737  		t.Fatalf("Write failed: %s", err)
  3738  	}
  3739  }
  3740  
  3741  func TestActiveSendMSSLessThanMTU(t *testing.T) {
  3742  	const maxPayload = 100
  3743  	c := context.New(t, 65535)
  3744  	defer c.Cleanup()
  3745  
  3746  	c.CreateConnectedWithRawOptions(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */, []byte{
  3747  		header.TCPOptionMSS, 4, byte(maxPayload / 256), byte(maxPayload % 256),
  3748  	})
  3749  	e2e.CheckBrokenUpWrite(t, c, maxPayload)
  3750  }
  3751  
  3752  func TestPassiveSendMSSLessThanMTU(t *testing.T) {
  3753  	const maxPayload = 100
  3754  	const mtu = 1200
  3755  	c := context.New(t, mtu)
  3756  	defer c.Cleanup()
  3757  
  3758  	// Create EP and start listening.
  3759  	wq := &waiter.Queue{}
  3760  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  3761  	if err != nil {
  3762  		t.Fatalf("NewEndpoint failed: %s", err)
  3763  	}
  3764  	defer ep.Close()
  3765  
  3766  	// Set the buffer size to a deterministic size so that we can check the
  3767  	// window scaling option.
  3768  	const rcvBufferSize = 0x20000
  3769  	ep.SocketOptions().SetReceiveBufferSize(rcvBufferSize*2, true /* notify */)
  3770  
  3771  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  3772  		t.Fatalf("Bind failed: %s", err)
  3773  	}
  3774  
  3775  	if err := ep.Listen(10); err != nil {
  3776  		t.Fatalf("Listen failed: %s", err)
  3777  	}
  3778  
  3779  	// Do 3-way handshake.
  3780  	c.PassiveConnect(maxPayload, -1, header.TCPSynOptions{MSS: mtu - header.IPv4MinimumSize - header.TCPMinimumSize})
  3781  
  3782  	// Try to accept the connection.
  3783  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  3784  	wq.EventRegister(&we)
  3785  	defer wq.EventUnregister(&we)
  3786  
  3787  	c.EP, _, err = ep.Accept(nil)
  3788  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  3789  		// Wait for connection to be established.
  3790  		select {
  3791  		case <-ch:
  3792  			c.EP, _, err = ep.Accept(nil)
  3793  			if err != nil {
  3794  				t.Fatalf("Accept failed: %s", err)
  3795  			}
  3796  
  3797  		case <-time.After(1 * time.Second):
  3798  			t.Fatalf("Timed out waiting for accept")
  3799  		}
  3800  	}
  3801  
  3802  	// Check that data gets properly segmented.
  3803  	e2e.CheckBrokenUpWrite(t, c, maxPayload)
  3804  }
  3805  
  3806  func TestSynCookiePassiveSendMSSLessThanMTU(t *testing.T) {
  3807  	const maxPayload = 536
  3808  	const mtu = 2000
  3809  	c := context.New(t, mtu)
  3810  	defer c.Cleanup()
  3811  
  3812  	opt := tcpip.TCPAlwaysUseSynCookies(true)
  3813  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  3814  		t.Fatalf("SetTransportProtocolOption(%d, &%T(%t)): %s", tcp.ProtocolNumber, opt, opt, err)
  3815  	}
  3816  
  3817  	// Create EP and start listening.
  3818  	wq := &waiter.Queue{}
  3819  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  3820  	if err != nil {
  3821  		t.Fatalf("NewEndpoint failed: %s", err)
  3822  	}
  3823  	defer ep.Close()
  3824  
  3825  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  3826  		t.Fatalf("Bind failed: %s", err)
  3827  	}
  3828  
  3829  	if err := ep.Listen(10); err != nil {
  3830  		t.Fatalf("Listen failed: %s", err)
  3831  	}
  3832  
  3833  	// Do 3-way handshake.
  3834  	c.PassiveConnect(maxPayload, -1, header.TCPSynOptions{MSS: mtu - header.IPv4MinimumSize - header.TCPMinimumSize})
  3835  
  3836  	// Try to accept the connection.
  3837  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  3838  	wq.EventRegister(&we)
  3839  	defer wq.EventUnregister(&we)
  3840  
  3841  	c.EP, _, err = ep.Accept(nil)
  3842  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  3843  		// Wait for connection to be established.
  3844  		select {
  3845  		case <-ch:
  3846  			c.EP, _, err = ep.Accept(nil)
  3847  			if err != nil {
  3848  				t.Fatalf("Accept failed: %s", err)
  3849  			}
  3850  
  3851  		case <-time.After(1 * time.Second):
  3852  			t.Fatalf("Timed out waiting for accept")
  3853  		}
  3854  	}
  3855  
  3856  	// Check that data gets properly segmented.
  3857  	e2e.CheckBrokenUpWrite(t, c, maxPayload)
  3858  }
  3859  
  3860  func TestSynOptionsOnActiveConnect(t *testing.T) {
  3861  	const mtu = 1400
  3862  	c := context.New(t, mtu)
  3863  	defer c.Cleanup()
  3864  
  3865  	// Create TCP endpoint.
  3866  	var err tcpip.Error
  3867  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  3868  	if err != nil {
  3869  		t.Fatalf("NewEndpoint failed: %s", err)
  3870  	}
  3871  
  3872  	// Set the buffer size to a deterministic size so that we can check the
  3873  	// window scaling option.
  3874  	const rcvBufferSize = 0x20000
  3875  	const wndScale = 3
  3876  	c.EP.SocketOptions().SetReceiveBufferSize(rcvBufferSize*2, true /* notify */)
  3877  
  3878  	// Start connection attempt.
  3879  	we, ch := waiter.NewChannelEntry(waiter.WritableEvents)
  3880  	c.WQ.EventRegister(&we)
  3881  	defer c.WQ.EventUnregister(&we)
  3882  
  3883  	{
  3884  		err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort})
  3885  		if d := cmp.Diff(&tcpip.ErrConnectStarted{}, err); d != "" {
  3886  			t.Fatalf("c.EP.Connect(...) mismatch (-want +got):\n%s", d)
  3887  		}
  3888  	}
  3889  
  3890  	// Receive SYN packet.
  3891  	b := c.GetPacket()
  3892  	defer b.Release()
  3893  	mss := uint16(mtu - header.IPv4MinimumSize - header.TCPMinimumSize)
  3894  	checker.IPv4(t, b,
  3895  		checker.TCP(
  3896  			checker.DstPort(context.TestPort),
  3897  			checker.TCPFlags(header.TCPFlagSyn),
  3898  			checker.TCPSynOptions(header.TCPSynOptions{MSS: mss, WS: wndScale}),
  3899  		),
  3900  	)
  3901  
  3902  	tcpHdr := header.TCP(header.IPv4(b.AsSlice()).Payload())
  3903  	c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
  3904  
  3905  	// Wait for retransmit.
  3906  	time.Sleep(1 * time.Second)
  3907  	v := c.GetPacket()
  3908  	defer v.Release()
  3909  	checker.IPv4(t, v, checker.TCP(
  3910  		checker.DstPort(context.TestPort),
  3911  		checker.TCPFlags(header.TCPFlagSyn),
  3912  		checker.SrcPort(tcpHdr.SourcePort()),
  3913  		checker.TCPSeqNum(tcpHdr.SequenceNumber()),
  3914  		checker.TCPSynOptions(header.TCPSynOptions{MSS: mss, WS: wndScale}),
  3915  	),
  3916  	)
  3917  
  3918  	// Send SYN-ACK.
  3919  	iss := seqnum.Value(context.TestInitialSequenceNumber)
  3920  	c.SendPacket(nil, &context.Headers{
  3921  		SrcPort: tcpHdr.DestinationPort(),
  3922  		DstPort: tcpHdr.SourcePort(),
  3923  		Flags:   header.TCPFlagSyn | header.TCPFlagAck,
  3924  		SeqNum:  iss,
  3925  		AckNum:  c.IRS.Add(1),
  3926  		RcvWnd:  30000,
  3927  	})
  3928  
  3929  	// Receive ACK packet.
  3930  	v = c.GetPacket()
  3931  	defer v.Release()
  3932  	checker.IPv4(t, v, checker.TCP(
  3933  		checker.DstPort(context.TestPort),
  3934  		checker.TCPFlags(header.TCPFlagAck),
  3935  		checker.TCPSeqNum(uint32(c.IRS)+1),
  3936  		checker.TCPAckNum(uint32(iss)+1),
  3937  	),
  3938  	)
  3939  
  3940  	// Wait for connection to be established.
  3941  	select {
  3942  	case <-ch:
  3943  		if err := c.EP.LastError(); err != nil {
  3944  			t.Fatalf("Connect failed: %s", err)
  3945  		}
  3946  	case <-time.After(1 * time.Second):
  3947  		t.Fatalf("Timed out waiting for connection")
  3948  	}
  3949  }
  3950  
  3951  func TestCloseListener(t *testing.T) {
  3952  	c := context.New(t, e2e.DefaultMTU)
  3953  	defer c.Cleanup()
  3954  
  3955  	// Create listener.
  3956  	var wq waiter.Queue
  3957  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq)
  3958  	if err != nil {
  3959  		t.Fatalf("NewEndpoint failed: %s", err)
  3960  	}
  3961  
  3962  	if err := ep.Bind(tcpip.FullAddress{}); err != nil {
  3963  		t.Fatalf("Bind failed: %s", err)
  3964  	}
  3965  
  3966  	if err := ep.Listen(10); err != nil {
  3967  		t.Fatalf("Listen failed: %s", err)
  3968  	}
  3969  
  3970  	// Close the listener and measure how long it takes.
  3971  	t0 := time.Now()
  3972  	ep.Close()
  3973  	if diff := time.Now().Sub(t0); diff > 3*time.Second {
  3974  		t.Fatalf("Took too long to close: %s", diff)
  3975  	}
  3976  }
  3977  
  3978  func TestReceiveOnResetConnection(t *testing.T) {
  3979  	c := context.New(t, e2e.DefaultMTU)
  3980  	defer c.Cleanup()
  3981  
  3982  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  3983  
  3984  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  3985  	// Send RST segment.
  3986  	c.SendPacket(nil, &context.Headers{
  3987  		SrcPort: context.TestPort,
  3988  		DstPort: c.Port,
  3989  		Flags:   header.TCPFlagRst,
  3990  		SeqNum:  iss,
  3991  		RcvWnd:  30000,
  3992  	})
  3993  
  3994  	// Try to read.
  3995  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  3996  	c.WQ.EventRegister(&we)
  3997  	defer c.WQ.EventUnregister(&we)
  3998  
  3999  loop:
  4000  	for {
  4001  		switch _, err := c.EP.Read(ioutil.Discard, tcpip.ReadOptions{}); err.(type) {
  4002  		case *tcpip.ErrWouldBlock:
  4003  			<-ch
  4004  			// Expect the state to be StateError and subsequent Reads to fail with HardError.
  4005  			_, err := c.EP.Read(ioutil.Discard, tcpip.ReadOptions{})
  4006  			if d := cmp.Diff(&tcpip.ErrConnectionReset{}, err); d != "" {
  4007  				t.Fatalf("c.EP.Read() mismatch (-want +got):\n%s", d)
  4008  			}
  4009  			break loop
  4010  		case *tcpip.ErrConnectionReset:
  4011  			break loop
  4012  		default:
  4013  			t.Fatalf("got c.EP.Read(nil) = %v, want = %s", err, &tcpip.ErrConnectionReset{})
  4014  		}
  4015  	}
  4016  
  4017  	if tcp.EndpointState(c.EP.State()) != tcp.StateError {
  4018  		t.Fatalf("got EP state is not StateError")
  4019  	}
  4020  
  4021  	checkValid := func() []error {
  4022  		var errors []error
  4023  		if got := c.Stack().Stats().TCP.EstablishedResets.Value(); got != 1 {
  4024  			errors = append(errors, fmt.Errorf("got stats.TCP.EstablishedResets.Value() = %d, want = 1", got))
  4025  		}
  4026  		if got := c.Stack().Stats().TCP.CurrentEstablished.Value(); got != 0 {
  4027  			errors = append(errors, fmt.Errorf("got stats.TCP.CurrentEstablished.Value() = %d, want = 0", got))
  4028  		}
  4029  		if got := c.Stack().Stats().TCP.CurrentConnected.Value(); got != 0 {
  4030  			errors = append(errors, fmt.Errorf("got stats.TCP.CurrentConnected.Value() = %d, want = 0", got))
  4031  		}
  4032  		return errors
  4033  	}
  4034  
  4035  	start := time.Now()
  4036  	for time.Since(start) < time.Minute && len(checkValid()) > 0 {
  4037  		time.Sleep(50 * time.Millisecond)
  4038  	}
  4039  	for _, err := range checkValid() {
  4040  		t.Error(err)
  4041  	}
  4042  }
  4043  
  4044  func TestSendOnResetConnection(t *testing.T) {
  4045  	c := context.New(t, e2e.DefaultMTU)
  4046  	defer c.Cleanup()
  4047  
  4048  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  4049  
  4050  	// Send RST segment.
  4051  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  4052  	c.SendPacket(nil, &context.Headers{
  4053  		SrcPort: context.TestPort,
  4054  		DstPort: c.Port,
  4055  		Flags:   header.TCPFlagRst,
  4056  		SeqNum:  iss,
  4057  		RcvWnd:  30000,
  4058  	})
  4059  
  4060  	// Wait for the RST to be received.
  4061  	time.Sleep(1 * time.Second)
  4062  
  4063  	// Try to write.
  4064  	var r bytes.Reader
  4065  	r.Reset(make([]byte, 10))
  4066  	_, err := c.EP.Write(&r, tcpip.WriteOptions{})
  4067  	if d := cmp.Diff(&tcpip.ErrConnectionReset{}, err); d != "" {
  4068  		t.Fatalf("c.EP.Write(...) mismatch (-want +got):\n%s", d)
  4069  	}
  4070  }
  4071  
  4072  // TestMaxRetransmitsTimeout tests if the connection is timed out after
  4073  // a segment has been retransmitted MaxRetries times.
  4074  func TestMaxRetransmitsTimeout(t *testing.T) {
  4075  	c := context.New(t, e2e.DefaultMTU)
  4076  	defer c.Cleanup()
  4077  
  4078  	const numRetries = 2
  4079  	opt := tcpip.TCPMaxRetriesOption(numRetries)
  4080  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  4081  		t.Fatalf("SetTransportProtocolOption(%d, &%T(%d)): %s", tcp.ProtocolNumber, opt, opt, err)
  4082  	}
  4083  
  4084  	// Wait for the connection to timeout after MaxRetries retransmits.
  4085  	initRTO := time.Second
  4086  	minRTOOpt := tcpip.TCPMinRTOOption(initRTO)
  4087  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &minRTOOpt); err != nil {
  4088  		t.Fatalf("SetTransportProtocolOption(%d, &%T(%d)): %s", tcp.ProtocolNumber, minRTOOpt, minRTOOpt, err)
  4089  	}
  4090  	c.CreateConnected(context.TestInitialSequenceNumber, 30000 /* rcvWnd */, -1 /* epRcvBuf */)
  4091  
  4092  	waitEntry, notifyCh := waiter.NewChannelEntry(waiter.EventHUp)
  4093  	c.WQ.EventRegister(&waitEntry)
  4094  	defer c.WQ.EventUnregister(&waitEntry)
  4095  
  4096  	var r bytes.Reader
  4097  	r.Reset(make([]byte, 1))
  4098  	_, err := c.EP.Write(&r, tcpip.WriteOptions{})
  4099  	if err != nil {
  4100  		t.Fatalf("Write failed: %s", err)
  4101  	}
  4102  
  4103  	// Expect first transmit and MaxRetries retransmits.
  4104  	for i := 0; i < numRetries+1; i++ {
  4105  		v := c.GetPacket()
  4106  		defer v.Release()
  4107  		checker.IPv4(t, v, checker.TCP(
  4108  			checker.DstPort(context.TestPort),
  4109  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagPsh),
  4110  		))
  4111  	}
  4112  	select {
  4113  	case <-notifyCh:
  4114  	case <-time.After((2 << numRetries) * initRTO):
  4115  		t.Fatalf("connection still alive after maximum retransmits.\n")
  4116  	}
  4117  
  4118  	// Send an ACK and expect a RST as the connection would have been closed.
  4119  	c.SendPacket(nil, &context.Headers{
  4120  		SrcPort: context.TestPort,
  4121  		DstPort: c.Port,
  4122  		Flags:   header.TCPFlagAck,
  4123  	})
  4124  
  4125  	v := c.GetPacket()
  4126  	defer v.Release()
  4127  	checker.IPv4(t, v, checker.TCP(
  4128  		checker.DstPort(context.TestPort),
  4129  		checker.TCPFlags(header.TCPFlagRst),
  4130  	))
  4131  
  4132  	if got := c.Stack().Stats().TCP.EstablishedTimedout.Value(); got != 1 {
  4133  		t.Errorf("got c.Stack().Stats().TCP.EstablishedTimedout.Value() = %d, want = 1", got)
  4134  	}
  4135  	if got := c.Stack().Stats().TCP.CurrentConnected.Value(); got != 0 {
  4136  		t.Errorf("got stats.TCP.CurrentConnected.Value() = %d, want = 0", got)
  4137  	}
  4138  }
  4139  
  4140  // TestMaxRTO tests if the retransmit interval caps to MaxRTO.
  4141  func TestMaxRTO(t *testing.T) {
  4142  	c := context.New(t, e2e.DefaultMTU)
  4143  	defer c.Cleanup()
  4144  
  4145  	rto := 1 * time.Second
  4146  	minRTOOpt := tcpip.TCPMinRTOOption(rto / 2)
  4147  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &minRTOOpt); err != nil {
  4148  		t.Fatalf("SetTransportProtocolOption(%d, &%T(%d)): %s", tcp.ProtocolNumber, minRTOOpt, minRTOOpt, err)
  4149  	}
  4150  	maxRTOOpt := tcpip.TCPMaxRTOOption(rto)
  4151  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &maxRTOOpt); err != nil {
  4152  		t.Fatalf("SetTransportProtocolOption(%d, &%T(%d)): %s", tcp.ProtocolNumber, maxRTOOpt, maxRTOOpt, err)
  4153  	}
  4154  
  4155  	c.CreateConnected(context.TestInitialSequenceNumber, 30000 /* rcvWnd */, -1 /* epRcvBuf */)
  4156  
  4157  	var r bytes.Reader
  4158  	r.Reset(make([]byte, 1))
  4159  	_, err := c.EP.Write(&r, tcpip.WriteOptions{})
  4160  	if err != nil {
  4161  		t.Fatalf("Write failed: %s", err)
  4162  	}
  4163  
  4164  	v := c.GetPacket()
  4165  	defer v.Release()
  4166  	checker.IPv4(t, v, checker.TCP(
  4167  		checker.DstPort(context.TestPort),
  4168  		checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  4169  	))
  4170  	const numRetransmits = 2
  4171  	for i := 0; i < numRetransmits; i++ {
  4172  		start := time.Now()
  4173  		v := c.GetPacket()
  4174  		defer v.Release()
  4175  		checker.IPv4(t, v, checker.TCP(
  4176  			checker.DstPort(context.TestPort),
  4177  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  4178  		))
  4179  		if elapsed := time.Since(start); elapsed.Round(time.Second).Seconds() != rto.Seconds() {
  4180  			newRto := float64(rto / time.Millisecond)
  4181  			if i == 0 {
  4182  				newRto /= 2
  4183  			}
  4184  			curRto := float64(elapsed.Round(time.Millisecond).Milliseconds())
  4185  			if math.Abs(newRto-curRto) > 10 {
  4186  				t.Errorf("Retransmit interval not capped to RTO(%v). %v", newRto, curRto)
  4187  			}
  4188  		}
  4189  	}
  4190  }
  4191  
  4192  // TestZeroSizedWriteRetransmit tests that a zero sized write should not
  4193  // result in a panic on an RTO as no segment should have been queued for
  4194  // a zero sized write.
  4195  func TestZeroSizedWriteRetransmit(t *testing.T) {
  4196  	c := context.New(t, e2e.DefaultMTU)
  4197  	defer c.Cleanup()
  4198  
  4199  	c.CreateConnected(context.TestInitialSequenceNumber, 30000 /* rcvWnd */, -1 /* epRcvBuf */)
  4200  
  4201  	var r bytes.Reader
  4202  	_, err := c.EP.Write(&r, tcpip.WriteOptions{})
  4203  	if err != nil {
  4204  		t.Fatalf("Write failed: %s", err)
  4205  	}
  4206  	// Now do a non-zero sized write to trigger actual sending of data.
  4207  	r.Reset(make([]byte, 1))
  4208  	_, err = c.EP.Write(&r, tcpip.WriteOptions{})
  4209  	if err != nil {
  4210  		t.Fatalf("Write failed: %s", err)
  4211  	}
  4212  	// Do not ACK the packet and expect an original transmit and a
  4213  	// retransmit. This should not cause a panic.
  4214  	for i := 0; i < 2; i++ {
  4215  		v := c.GetPacket()
  4216  		defer v.Release()
  4217  		checker.IPv4(t, v, checker.TCP(
  4218  			checker.DstPort(context.TestPort),
  4219  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  4220  		))
  4221  	}
  4222  }
  4223  
  4224  // TestRetransmitIPv4IDUniqueness tests that the IPv4 Identification field is
  4225  // unique on retransmits.
  4226  func TestRetransmitIPv4IDUniqueness(t *testing.T) {
  4227  	for _, tc := range []struct {
  4228  		name string
  4229  		size int
  4230  	}{
  4231  		{"1Byte", 1},
  4232  		{"512Bytes", 512},
  4233  	} {
  4234  		t.Run(tc.name, func(t *testing.T) {
  4235  			c := context.New(t, e2e.DefaultMTU)
  4236  			defer c.Cleanup()
  4237  
  4238  			minRTOOpt := tcpip.TCPMinRTOOption(time.Second)
  4239  			if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &minRTOOpt); err != nil {
  4240  				t.Fatalf("SetTransportProtocolOption(%d, &%T(%d)): %s", tcp.ProtocolNumber, minRTOOpt, minRTOOpt, err)
  4241  			}
  4242  			c.CreateConnected(context.TestInitialSequenceNumber, 30000 /* rcvWnd */, -1 /* epRcvBuf */)
  4243  
  4244  			// Disabling PMTU discovery causes all packets sent from this socket to
  4245  			// have DF=0. This needs to be done because the IPv4 ID uniqueness
  4246  			// applies only to non-atomic IPv4 datagrams as defined in RFC 6864
  4247  			// Section 4, and datagrams with DF=0 are non-atomic.
  4248  			if err := c.EP.SetSockOptInt(tcpip.MTUDiscoverOption, int(tcpip.PMTUDiscoveryDont)); err != nil {
  4249  				t.Fatalf("disabling PMTU discovery via sockopt to force DF=0 failed: %s", err)
  4250  			}
  4251  
  4252  			var r bytes.Reader
  4253  			r.Reset(make([]byte, tc.size))
  4254  			if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  4255  				t.Fatalf("Write failed: %s", err)
  4256  			}
  4257  			pkt := c.GetPacket()
  4258  			defer pkt.Release()
  4259  			checker.IPv4(t, pkt,
  4260  				checker.FragmentFlags(0),
  4261  				checker.TCP(
  4262  					checker.DstPort(context.TestPort),
  4263  					checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  4264  				),
  4265  			)
  4266  			idSet := map[uint16]struct{}{header.IPv4(pkt.AsSlice()).ID(): {}}
  4267  			// Expect two retransmitted packets, and that all packets received have
  4268  			// unique IPv4 ID values.
  4269  			for i := 0; i <= 2; i++ {
  4270  				pkt := c.GetPacket()
  4271  				defer pkt.Release()
  4272  				checker.IPv4(t, pkt,
  4273  					checker.FragmentFlags(0),
  4274  					checker.TCP(
  4275  						checker.DstPort(context.TestPort),
  4276  						checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  4277  					),
  4278  				)
  4279  				id := header.IPv4(pkt.AsSlice()).ID()
  4280  				if _, exists := idSet[id]; exists {
  4281  					t.Fatalf("duplicate IPv4 ID=%d found in retransmitted packet", id)
  4282  				}
  4283  				idSet[id] = struct{}{}
  4284  			}
  4285  		})
  4286  	}
  4287  }
  4288  
  4289  func TestFinImmediately(t *testing.T) {
  4290  	c := context.New(t, e2e.DefaultMTU)
  4291  	defer c.Cleanup()
  4292  
  4293  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  4294  
  4295  	// Shutdown immediately, check that we get a FIN.
  4296  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  4297  		t.Fatalf("Shutdown failed: %s", err)
  4298  	}
  4299  
  4300  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  4301  	v := c.GetPacket()
  4302  	defer v.Release()
  4303  	checker.IPv4(t, v,
  4304  		checker.PayloadLen(header.TCPMinimumSize),
  4305  		checker.TCP(
  4306  			checker.DstPort(context.TestPort),
  4307  			checker.TCPSeqNum(uint32(c.IRS)+1),
  4308  			checker.TCPAckNum(uint32(iss)),
  4309  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  4310  		),
  4311  	)
  4312  
  4313  	// Ack and send FIN as well.
  4314  	c.SendPacket(nil, &context.Headers{
  4315  		SrcPort: context.TestPort,
  4316  		DstPort: c.Port,
  4317  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  4318  		SeqNum:  iss,
  4319  		AckNum:  c.IRS.Add(2),
  4320  		RcvWnd:  30000,
  4321  	})
  4322  
  4323  	// Check that the stack acks the FIN.
  4324  	v = c.GetPacket()
  4325  	defer v.Release()
  4326  	checker.IPv4(t, v,
  4327  		checker.PayloadLen(header.TCPMinimumSize),
  4328  		checker.TCP(
  4329  			checker.DstPort(context.TestPort),
  4330  			checker.TCPSeqNum(uint32(c.IRS)+2),
  4331  			checker.TCPAckNum(uint32(iss)+1),
  4332  			checker.TCPFlags(header.TCPFlagAck),
  4333  		),
  4334  	)
  4335  }
  4336  
  4337  func TestFinRetransmit(t *testing.T) {
  4338  	c := context.New(t, e2e.DefaultMTU)
  4339  	defer c.Cleanup()
  4340  
  4341  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  4342  
  4343  	// Shutdown immediately, check that we get a FIN.
  4344  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  4345  		t.Fatalf("Shutdown failed: %s", err)
  4346  	}
  4347  
  4348  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  4349  
  4350  	v := c.GetPacket()
  4351  	defer v.Release()
  4352  	checker.IPv4(t, v,
  4353  		checker.PayloadLen(header.TCPMinimumSize),
  4354  		checker.TCP(
  4355  			checker.DstPort(context.TestPort),
  4356  			checker.TCPSeqNum(uint32(c.IRS)+1),
  4357  			checker.TCPAckNum(uint32(iss)),
  4358  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  4359  		),
  4360  	)
  4361  
  4362  	// Don't acknowledge yet. We should get a retransmit of the FIN.
  4363  	v = c.GetPacket()
  4364  	defer v.Release()
  4365  	checker.IPv4(t, v,
  4366  		checker.PayloadLen(header.TCPMinimumSize),
  4367  		checker.TCP(
  4368  			checker.DstPort(context.TestPort),
  4369  			checker.TCPSeqNum(uint32(c.IRS)+1),
  4370  			checker.TCPAckNum(uint32(iss)),
  4371  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  4372  		),
  4373  	)
  4374  
  4375  	// Ack and send FIN as well.
  4376  	c.SendPacket(nil, &context.Headers{
  4377  		SrcPort: context.TestPort,
  4378  		DstPort: c.Port,
  4379  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  4380  		SeqNum:  iss,
  4381  		AckNum:  c.IRS.Add(2),
  4382  		RcvWnd:  30000,
  4383  	})
  4384  
  4385  	// Check that the stack acks the FIN.
  4386  	v = c.GetPacket()
  4387  	defer v.Release()
  4388  	checker.IPv4(t, v,
  4389  		checker.PayloadLen(header.TCPMinimumSize),
  4390  		checker.TCP(
  4391  			checker.DstPort(context.TestPort),
  4392  			checker.TCPSeqNum(uint32(c.IRS)+2),
  4393  			checker.TCPAckNum(uint32(iss)+1),
  4394  			checker.TCPFlags(header.TCPFlagAck),
  4395  		),
  4396  	)
  4397  }
  4398  
  4399  func TestFinWithNoPendingData(t *testing.T) {
  4400  	c := context.New(t, e2e.DefaultMTU)
  4401  	defer c.Cleanup()
  4402  
  4403  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  4404  
  4405  	// Write something out, and have it acknowledged.
  4406  	view := make([]byte, 10)
  4407  	var r bytes.Reader
  4408  	r.Reset(view)
  4409  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  4410  		t.Fatalf("Write failed: %s", err)
  4411  	}
  4412  
  4413  	next := uint32(c.IRS) + 1
  4414  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  4415  	v := c.GetPacket()
  4416  	defer v.Release()
  4417  	checker.IPv4(t, v,
  4418  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  4419  		checker.TCP(
  4420  			checker.DstPort(context.TestPort),
  4421  			checker.TCPSeqNum(next),
  4422  			checker.TCPAckNum(uint32(iss)),
  4423  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  4424  		),
  4425  	)
  4426  	next += uint32(len(view))
  4427  
  4428  	c.SendPacket(nil, &context.Headers{
  4429  		SrcPort: context.TestPort,
  4430  		DstPort: c.Port,
  4431  		Flags:   header.TCPFlagAck,
  4432  		SeqNum:  iss,
  4433  		AckNum:  seqnum.Value(next),
  4434  		RcvWnd:  30000,
  4435  	})
  4436  
  4437  	// Shutdown, check that we get a FIN.
  4438  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  4439  		t.Fatalf("Shutdown failed: %s", err)
  4440  	}
  4441  
  4442  	v = c.GetPacket()
  4443  	defer v.Release()
  4444  	checker.IPv4(t, v,
  4445  		checker.PayloadLen(header.TCPMinimumSize),
  4446  		checker.TCP(
  4447  			checker.DstPort(context.TestPort),
  4448  			checker.TCPSeqNum(next),
  4449  			checker.TCPAckNum(uint32(iss)),
  4450  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  4451  		),
  4452  	)
  4453  	next++
  4454  
  4455  	// Ack and send FIN as well.
  4456  	c.SendPacket(nil, &context.Headers{
  4457  		SrcPort: context.TestPort,
  4458  		DstPort: c.Port,
  4459  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  4460  		SeqNum:  iss,
  4461  		AckNum:  seqnum.Value(next),
  4462  		RcvWnd:  30000,
  4463  	})
  4464  
  4465  	// Check that the stack acks the FIN.
  4466  	v = c.GetPacket()
  4467  	defer v.Release()
  4468  	checker.IPv4(t, v,
  4469  		checker.PayloadLen(header.TCPMinimumSize),
  4470  		checker.TCP(
  4471  			checker.DstPort(context.TestPort),
  4472  			checker.TCPSeqNum(next),
  4473  			checker.TCPAckNum(uint32(iss)+1),
  4474  			checker.TCPFlags(header.TCPFlagAck),
  4475  		),
  4476  	)
  4477  }
  4478  
  4479  func TestFinWithPendingDataCwndFull(t *testing.T) {
  4480  	c := context.New(t, e2e.DefaultMTU)
  4481  	defer c.Cleanup()
  4482  
  4483  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  4484  
  4485  	// Write enough segments to fill the congestion window before ACK'ing
  4486  	// any of them.
  4487  	view := make([]byte, 10)
  4488  	var r bytes.Reader
  4489  	for i := tcp.InitialCwnd; i > 0; i-- {
  4490  		r.Reset(view)
  4491  		if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  4492  			t.Fatalf("Write failed: %s", err)
  4493  		}
  4494  	}
  4495  
  4496  	next := uint32(c.IRS) + 1
  4497  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  4498  	for i := tcp.InitialCwnd; i > 0; i-- {
  4499  		v := c.GetPacket()
  4500  		defer v.Release()
  4501  		checker.IPv4(t, v,
  4502  			checker.PayloadLen(len(view)+header.TCPMinimumSize),
  4503  			checker.TCP(
  4504  				checker.DstPort(context.TestPort),
  4505  				checker.TCPSeqNum(next),
  4506  				checker.TCPAckNum(uint32(iss)),
  4507  				checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  4508  			),
  4509  		)
  4510  		next += uint32(len(view))
  4511  	}
  4512  
  4513  	// Shutdown the connection, check that the FIN segment isn't sent
  4514  	// because the congestion window doesn't allow it. Wait until a
  4515  	// retransmit is received.
  4516  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  4517  		t.Fatalf("Shutdown failed: %s", err)
  4518  	}
  4519  
  4520  	v := c.GetPacket()
  4521  	defer v.Release()
  4522  	checker.IPv4(t, v,
  4523  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  4524  		checker.TCP(
  4525  			checker.DstPort(context.TestPort),
  4526  			checker.TCPSeqNum(uint32(c.IRS)+1),
  4527  			checker.TCPAckNum(uint32(iss)),
  4528  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  4529  		),
  4530  	)
  4531  
  4532  	// Send the ACK that will allow the FIN to be sent as well.
  4533  	c.SendPacket(nil, &context.Headers{
  4534  		SrcPort: context.TestPort,
  4535  		DstPort: c.Port,
  4536  		Flags:   header.TCPFlagAck,
  4537  		SeqNum:  iss,
  4538  		AckNum:  seqnum.Value(next),
  4539  		RcvWnd:  30000,
  4540  	})
  4541  
  4542  	v = c.GetPacket()
  4543  	defer v.Release()
  4544  	checker.IPv4(t, v,
  4545  		checker.PayloadLen(header.TCPMinimumSize),
  4546  		checker.TCP(
  4547  			checker.DstPort(context.TestPort),
  4548  			checker.TCPSeqNum(next),
  4549  			checker.TCPAckNum(uint32(iss)),
  4550  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  4551  		),
  4552  	)
  4553  	next++
  4554  
  4555  	// Send a FIN that acknowledges everything. Get an ACK back.
  4556  	c.SendPacket(nil, &context.Headers{
  4557  		SrcPort: context.TestPort,
  4558  		DstPort: c.Port,
  4559  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  4560  		SeqNum:  iss,
  4561  		AckNum:  seqnum.Value(next),
  4562  		RcvWnd:  30000,
  4563  	})
  4564  
  4565  	v = c.GetPacket()
  4566  	defer v.Release()
  4567  	checker.IPv4(t, v,
  4568  		checker.PayloadLen(header.TCPMinimumSize),
  4569  		checker.TCP(
  4570  			checker.DstPort(context.TestPort),
  4571  			checker.TCPSeqNum(next),
  4572  			checker.TCPAckNum(uint32(iss)+1),
  4573  			checker.TCPFlags(header.TCPFlagAck),
  4574  		),
  4575  	)
  4576  }
  4577  
  4578  func TestFinWithPendingData(t *testing.T) {
  4579  	c := context.New(t, e2e.DefaultMTU)
  4580  	defer c.Cleanup()
  4581  
  4582  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  4583  
  4584  	// Write something out, and acknowledge it to get cwnd to 2.
  4585  	view := make([]byte, 10)
  4586  	var r bytes.Reader
  4587  	r.Reset(view)
  4588  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  4589  		t.Fatalf("Write failed: %s", err)
  4590  	}
  4591  
  4592  	next := uint32(c.IRS) + 1
  4593  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  4594  	v := c.GetPacket()
  4595  	defer v.Release()
  4596  	checker.IPv4(t, v,
  4597  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  4598  		checker.TCP(
  4599  			checker.DstPort(context.TestPort),
  4600  			checker.TCPSeqNum(next),
  4601  			checker.TCPAckNum(uint32(iss)),
  4602  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  4603  		),
  4604  	)
  4605  	next += uint32(len(view))
  4606  
  4607  	c.SendPacket(nil, &context.Headers{
  4608  		SrcPort: context.TestPort,
  4609  		DstPort: c.Port,
  4610  		Flags:   header.TCPFlagAck,
  4611  		SeqNum:  iss,
  4612  		AckNum:  seqnum.Value(next),
  4613  		RcvWnd:  30000,
  4614  	})
  4615  
  4616  	// Write new data, but don't acknowledge it.
  4617  	r.Reset(view)
  4618  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  4619  		t.Fatalf("Write failed: %s", err)
  4620  	}
  4621  
  4622  	v = c.GetPacket()
  4623  	defer v.Release()
  4624  	checker.IPv4(t, v,
  4625  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  4626  		checker.TCP(
  4627  			checker.DstPort(context.TestPort),
  4628  			checker.TCPSeqNum(next),
  4629  			checker.TCPAckNum(uint32(iss)),
  4630  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  4631  		),
  4632  	)
  4633  	next += uint32(len(view))
  4634  
  4635  	// Shutdown the connection, check that we do get a FIN.
  4636  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  4637  		t.Fatalf("Shutdown failed: %s", err)
  4638  	}
  4639  
  4640  	v = c.GetPacket()
  4641  	defer v.Release()
  4642  	checker.IPv4(t, v,
  4643  		checker.PayloadLen(header.TCPMinimumSize),
  4644  		checker.TCP(
  4645  			checker.DstPort(context.TestPort),
  4646  			checker.TCPSeqNum(next),
  4647  			checker.TCPAckNum(uint32(iss)),
  4648  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  4649  		),
  4650  	)
  4651  	next++
  4652  
  4653  	// Send a FIN that acknowledges everything. Get an ACK back.
  4654  	c.SendPacket(nil, &context.Headers{
  4655  		SrcPort: context.TestPort,
  4656  		DstPort: c.Port,
  4657  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  4658  		SeqNum:  iss,
  4659  		AckNum:  seqnum.Value(next),
  4660  		RcvWnd:  30000,
  4661  	})
  4662  
  4663  	v = c.GetPacket()
  4664  	defer v.Release()
  4665  	checker.IPv4(t, v,
  4666  		checker.PayloadLen(header.TCPMinimumSize),
  4667  		checker.TCP(
  4668  			checker.DstPort(context.TestPort),
  4669  			checker.TCPSeqNum(next),
  4670  			checker.TCPAckNum(uint32(iss)+1),
  4671  			checker.TCPFlags(header.TCPFlagAck),
  4672  		),
  4673  	)
  4674  }
  4675  
  4676  func TestFinWithPartialAck(t *testing.T) {
  4677  	c := context.New(t, e2e.DefaultMTU)
  4678  	defer c.Cleanup()
  4679  
  4680  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  4681  
  4682  	// Write something out, and acknowledge it to get cwnd to 2. Also send
  4683  	// FIN from the test side.
  4684  	view := make([]byte, 10)
  4685  	var r bytes.Reader
  4686  	r.Reset(view)
  4687  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  4688  		t.Fatalf("Write failed: %s", err)
  4689  	}
  4690  
  4691  	next := uint32(c.IRS) + 1
  4692  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  4693  	v := c.GetPacket()
  4694  	defer v.Release()
  4695  	checker.IPv4(t, v,
  4696  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  4697  		checker.TCP(
  4698  			checker.DstPort(context.TestPort),
  4699  			checker.TCPSeqNum(next),
  4700  			checker.TCPAckNum(uint32(iss)),
  4701  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  4702  		),
  4703  	)
  4704  	next += uint32(len(view))
  4705  
  4706  	c.SendPacket(nil, &context.Headers{
  4707  		SrcPort: context.TestPort,
  4708  		DstPort: c.Port,
  4709  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  4710  		SeqNum:  iss,
  4711  		AckNum:  seqnum.Value(next),
  4712  		RcvWnd:  30000,
  4713  	})
  4714  
  4715  	// Check that we get an ACK for the fin.
  4716  	v = c.GetPacket()
  4717  	defer v.Release()
  4718  	checker.IPv4(t, v,
  4719  		checker.PayloadLen(header.TCPMinimumSize),
  4720  		checker.TCP(
  4721  			checker.DstPort(context.TestPort),
  4722  			checker.TCPSeqNum(next),
  4723  			checker.TCPAckNum(uint32(iss)+1),
  4724  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  4725  		),
  4726  	)
  4727  
  4728  	// Write new data, but don't acknowledge it.
  4729  	r.Reset(view)
  4730  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  4731  		t.Fatalf("Write failed: %s", err)
  4732  	}
  4733  
  4734  	v = c.GetPacket()
  4735  	defer v.Release()
  4736  	checker.IPv4(t, v,
  4737  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  4738  		checker.TCP(
  4739  			checker.DstPort(context.TestPort),
  4740  			checker.TCPSeqNum(next),
  4741  			checker.TCPAckNum(uint32(iss)+1),
  4742  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  4743  		),
  4744  	)
  4745  	next += uint32(len(view))
  4746  
  4747  	// Shutdown the connection, check that we do get a FIN.
  4748  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  4749  		t.Fatalf("Shutdown failed: %s", err)
  4750  	}
  4751  
  4752  	v = c.GetPacket()
  4753  	defer v.Release()
  4754  	checker.IPv4(t, v,
  4755  		checker.PayloadLen(header.TCPMinimumSize),
  4756  		checker.TCP(
  4757  			checker.DstPort(context.TestPort),
  4758  			checker.TCPSeqNum(next),
  4759  			checker.TCPAckNum(uint32(iss)+1),
  4760  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  4761  		),
  4762  	)
  4763  	next++
  4764  
  4765  	// Send an ACK for the data, but not for the FIN yet.
  4766  	c.SendPacket(nil, &context.Headers{
  4767  		SrcPort: context.TestPort,
  4768  		DstPort: c.Port,
  4769  		Flags:   header.TCPFlagAck,
  4770  		SeqNum:  iss.Add(1),
  4771  		AckNum:  seqnum.Value(next - 1),
  4772  		RcvWnd:  30000,
  4773  	})
  4774  
  4775  	// Check that we don't get a retransmit of the FIN.
  4776  	c.CheckNoPacketTimeout("FIN retransmitted when data was ack'd", 100*time.Millisecond)
  4777  
  4778  	// Ack the FIN.
  4779  	c.SendPacket(nil, &context.Headers{
  4780  		SrcPort: context.TestPort,
  4781  		DstPort: c.Port,
  4782  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  4783  		SeqNum:  iss.Add(1),
  4784  		AckNum:  seqnum.Value(next),
  4785  		RcvWnd:  30000,
  4786  	})
  4787  }
  4788  
  4789  func TestUpdateListenBacklog(t *testing.T) {
  4790  	c := context.New(t, e2e.DefaultMTU)
  4791  	defer c.Cleanup()
  4792  
  4793  	// Create listener.
  4794  	var wq waiter.Queue
  4795  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq)
  4796  	if err != nil {
  4797  		t.Fatalf("NewEndpoint failed: %s", err)
  4798  	}
  4799  
  4800  	if err := ep.Bind(tcpip.FullAddress{}); err != nil {
  4801  		t.Fatalf("Bind failed: %s", err)
  4802  	}
  4803  
  4804  	if err := ep.Listen(10); err != nil {
  4805  		t.Fatalf("Listen failed: %s", err)
  4806  	}
  4807  
  4808  	// Update the backlog with another Listen() on the same endpoint.
  4809  	if err := ep.Listen(20); err != nil {
  4810  		t.Fatalf("Listen failed to update backlog: %s", err)
  4811  	}
  4812  
  4813  	ep.Close()
  4814  }
  4815  
  4816  func scaledSendWindow(t *testing.T, scale uint8) {
  4817  	// This test ensures that the endpoint is using the right scaling by
  4818  	// sending a buffer that is larger than the window size, and ensuring
  4819  	// that the endpoint doesn't send more than allowed.
  4820  	c := context.New(t, e2e.DefaultMTU)
  4821  	defer c.Cleanup()
  4822  
  4823  	maxPayload := e2e.DefaultMTU - header.IPv4MinimumSize - header.TCPMinimumSize
  4824  	c.CreateConnectedWithRawOptions(context.TestInitialSequenceNumber, 0, -1 /* epRcvBuf */, []byte{
  4825  		header.TCPOptionMSS, 4, byte(maxPayload / 256), byte(maxPayload % 256),
  4826  		header.TCPOptionWS, 3, scale, header.TCPOptionNOP,
  4827  	})
  4828  
  4829  	// Open up the window with a scaled value.
  4830  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  4831  	c.SendPacket(nil, &context.Headers{
  4832  		SrcPort: context.TestPort,
  4833  		DstPort: c.Port,
  4834  		Flags:   header.TCPFlagAck,
  4835  		SeqNum:  iss,
  4836  		AckNum:  c.IRS.Add(1),
  4837  		RcvWnd:  1,
  4838  	})
  4839  
  4840  	// Send some data. Check that it's capped by the window size.
  4841  	view := make([]byte, 65535)
  4842  	var r bytes.Reader
  4843  	r.Reset(view)
  4844  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  4845  		t.Fatalf("Write failed: %s", err)
  4846  	}
  4847  
  4848  	// Check that only data that fits in the scaled window is sent.
  4849  	v := c.GetPacket()
  4850  	defer v.Release()
  4851  	checker.IPv4(t, v,
  4852  		checker.PayloadLen((1<<scale)+header.TCPMinimumSize),
  4853  		checker.TCP(
  4854  			checker.DstPort(context.TestPort),
  4855  			checker.TCPSeqNum(uint32(c.IRS)+1),
  4856  			checker.TCPAckNum(uint32(iss)),
  4857  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  4858  		),
  4859  	)
  4860  
  4861  	// Reset the connection to free resources.
  4862  	c.SendPacket(nil, &context.Headers{
  4863  		SrcPort: context.TestPort,
  4864  		DstPort: c.Port,
  4865  		Flags:   header.TCPFlagRst,
  4866  		SeqNum:  iss,
  4867  	})
  4868  }
  4869  
  4870  func TestScaledSendWindow(t *testing.T) {
  4871  	for scale := uint8(0); scale <= 14; scale++ {
  4872  		scaledSendWindow(t, scale)
  4873  	}
  4874  }
  4875  
  4876  func TestReceivedValidSegmentCountIncrement(t *testing.T) {
  4877  	c := context.New(t, e2e.DefaultMTU)
  4878  	defer c.Cleanup()
  4879  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  4880  	stats := c.Stack().Stats()
  4881  	want := stats.TCP.ValidSegmentsReceived.Value() + 1
  4882  
  4883  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  4884  	c.SendPacket(nil, &context.Headers{
  4885  		SrcPort: context.TestPort,
  4886  		DstPort: c.Port,
  4887  		Flags:   header.TCPFlagAck,
  4888  		SeqNum:  iss,
  4889  		AckNum:  c.IRS.Add(1),
  4890  		RcvWnd:  30000,
  4891  	})
  4892  
  4893  	if got := stats.TCP.ValidSegmentsReceived.Value(); got != want {
  4894  		t.Errorf("got stats.TCP.ValidSegmentsReceived.Value() = %d, want = %d", got, want)
  4895  	}
  4896  	if got := c.EP.Stats().(*tcp.Stats).SegmentsReceived.Value(); got != want {
  4897  		t.Errorf("got EP stats Stats.SegmentsReceived = %d, want = %d", got, want)
  4898  	}
  4899  	// Ensure there were no errors during handshake. If these stats have
  4900  	// incremented, then the connection should not have been established.
  4901  	if got := c.EP.Stats().(*tcp.Stats).SendErrors.NoRoute.Value(); got != 0 {
  4902  		t.Errorf("got EP stats Stats.SendErrors.NoRoute = %d, want = %d", got, 0)
  4903  	}
  4904  }
  4905  
  4906  func TestReceivedInvalidSegmentCountIncrement(t *testing.T) {
  4907  	c := context.New(t, e2e.DefaultMTU)
  4908  	defer c.Cleanup()
  4909  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  4910  	stats := c.Stack().Stats()
  4911  	want := stats.TCP.InvalidSegmentsReceived.Value() + 1
  4912  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  4913  	buf := c.BuildSegment(nil, &context.Headers{
  4914  		SrcPort: context.TestPort,
  4915  		DstPort: c.Port,
  4916  		Flags:   header.TCPFlagAck,
  4917  		SeqNum:  iss,
  4918  		AckNum:  c.IRS.Add(1),
  4919  		RcvWnd:  30000,
  4920  	})
  4921  	defer buf.Release()
  4922  	tcpbuf := buf.Flatten()
  4923  	tcpbuf[header.IPv4MinimumSize+header.TCPDataOffset] = ((header.TCPMinimumSize - 1) / 4) << 4
  4924  
  4925  	segbuf := buffer.MakeWithData(tcpbuf)
  4926  	c.SendSegment(segbuf)
  4927  
  4928  	if got := stats.TCP.InvalidSegmentsReceived.Value(); got != want {
  4929  		t.Errorf("got stats.TCP.InvalidSegmentsReceived.Value() = %d, want = %d", got, want)
  4930  	}
  4931  	if got := c.EP.Stats().(*tcp.Stats).ReceiveErrors.MalformedPacketsReceived.Value(); got != want {
  4932  		t.Errorf("got EP Stats.ReceiveErrors.MalformedPacketsReceived stats = %d, want = %d", got, want)
  4933  	}
  4934  }
  4935  
  4936  func TestReceivedIncorrectChecksumIncrement(t *testing.T) {
  4937  	c := context.New(t, e2e.DefaultMTU)
  4938  	defer c.Cleanup()
  4939  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  4940  	stats := c.Stack().Stats()
  4941  	want := stats.TCP.ChecksumErrors.Value() + 1
  4942  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  4943  	buf := c.BuildSegment([]byte{0x1, 0x2, 0x3}, &context.Headers{
  4944  		SrcPort: context.TestPort,
  4945  		DstPort: c.Port,
  4946  		Flags:   header.TCPFlagAck,
  4947  		SeqNum:  iss,
  4948  		AckNum:  c.IRS.Add(1),
  4949  		RcvWnd:  30000,
  4950  	})
  4951  	defer buf.Release()
  4952  	tcpbuf := buf.Flatten()
  4953  	// Overwrite a byte in the payload which should cause checksum
  4954  	// verification to fail.
  4955  	tcpbuf[header.IPv4MinimumSize+((tcpbuf[header.IPv4MinimumSize+header.TCPDataOffset]>>4)*4)] = 0x4
  4956  
  4957  	segbuf := buffer.MakeWithData(tcpbuf)
  4958  	defer segbuf.Release()
  4959  	c.SendSegment(buffer.MakeWithData(tcpbuf))
  4960  
  4961  	if got := stats.TCP.ChecksumErrors.Value(); got != want {
  4962  		t.Errorf("got stats.TCP.ChecksumErrors.Value() = %d, want = %d", got, want)
  4963  	}
  4964  	if got := c.EP.Stats().(*tcp.Stats).ReceiveErrors.ChecksumErrors.Value(); got != want {
  4965  		t.Errorf("got EP stats Stats.ReceiveErrors.ChecksumErrors = %d, want = %d", got, want)
  4966  	}
  4967  }
  4968  
  4969  func TestReceivedSegmentQueuing(t *testing.T) {
  4970  	// This test sends 200 segments containing a few bytes each to an
  4971  	// endpoint and checks that they're all received and acknowledged by
  4972  	// the endpoint, that is, that none of the segments are dropped by
  4973  	// internal queues.
  4974  	c := context.New(t, e2e.DefaultMTU)
  4975  	defer c.Cleanup()
  4976  
  4977  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  4978  
  4979  	// Send 200 segments.
  4980  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  4981  	data := []byte{1, 2, 3}
  4982  	for i := 0; i < 200; i++ {
  4983  		c.SendPacket(data, &context.Headers{
  4984  			SrcPort: context.TestPort,
  4985  			DstPort: c.Port,
  4986  			Flags:   header.TCPFlagAck,
  4987  			SeqNum:  iss.Add(seqnum.Size(i * len(data))),
  4988  			AckNum:  c.IRS.Add(1),
  4989  			RcvWnd:  30000,
  4990  		})
  4991  	}
  4992  
  4993  	// Receive ACKs for all segments.
  4994  	last := iss.Add(seqnum.Size(200 * len(data)))
  4995  	for {
  4996  		b := c.GetPacket()
  4997  		defer b.Release()
  4998  		checker.IPv4(t, b,
  4999  			checker.TCP(
  5000  				checker.DstPort(context.TestPort),
  5001  				checker.TCPSeqNum(uint32(c.IRS)+1),
  5002  				checker.TCPFlags(header.TCPFlagAck),
  5003  			),
  5004  		)
  5005  		tcpHdr := header.TCP(header.IPv4(b.AsSlice()).Payload())
  5006  		ack := seqnum.Value(tcpHdr.AckNumber())
  5007  		if ack == last {
  5008  			break
  5009  		}
  5010  
  5011  		if last.LessThan(ack) {
  5012  			t.Fatalf("Acknowledge (%v) beyond the expected (%v)", ack, last)
  5013  		}
  5014  	}
  5015  }
  5016  
  5017  func TestReadAfterClosedState(t *testing.T) {
  5018  	// This test ensures that calling Read() or Peek() after the endpoint
  5019  	// has transitioned to closedState still works if there is pending
  5020  	// data. To transition to stateClosed without calling Close(), we must
  5021  	// shutdown the send path and the peer must send its own FIN.
  5022  	c := context.New(t, e2e.DefaultMTU)
  5023  	defer c.Cleanup()
  5024  
  5025  	// Set TCPTimeWaitTimeout to 1 seconds so that sockets are marked closed
  5026  	// after 1 second in TIME_WAIT state.
  5027  	tcpTimeWaitTimeout := 1 * time.Second
  5028  	opt := tcpip.TCPTimeWaitTimeoutOption(tcpTimeWaitTimeout)
  5029  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  5030  		t.Fatalf("SetTransportProtocolOption(%d, &%T(%d)): %s", tcp.ProtocolNumber, opt, opt, err)
  5031  	}
  5032  
  5033  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  5034  
  5035  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  5036  	c.WQ.EventRegister(&we)
  5037  	defer c.WQ.EventUnregister(&we)
  5038  
  5039  	ept := endpointTester{c.EP}
  5040  	ept.CheckReadError(t, &tcpip.ErrWouldBlock{})
  5041  
  5042  	// Shutdown immediately for write, check that we get a FIN.
  5043  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  5044  		t.Fatalf("Shutdown failed: %s", err)
  5045  	}
  5046  
  5047  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  5048  	b := c.GetPacket()
  5049  	defer b.Release()
  5050  	checker.IPv4(t, b,
  5051  		checker.PayloadLen(header.TCPMinimumSize),
  5052  		checker.TCP(
  5053  			checker.DstPort(context.TestPort),
  5054  			checker.TCPSeqNum(uint32(c.IRS)+1),
  5055  			checker.TCPAckNum(uint32(iss)),
  5056  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  5057  		),
  5058  	)
  5059  
  5060  	if got, want := tcp.EndpointState(c.EP.State()), tcp.StateFinWait1; got != want {
  5061  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
  5062  	}
  5063  
  5064  	// Send some data and acknowledge the FIN.
  5065  	data := []byte{1, 2, 3}
  5066  	c.SendPacket(data, &context.Headers{
  5067  		SrcPort: context.TestPort,
  5068  		DstPort: c.Port,
  5069  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  5070  		SeqNum:  iss,
  5071  		AckNum:  c.IRS.Add(2),
  5072  		RcvWnd:  30000,
  5073  	})
  5074  
  5075  	// Check that ACK is received.
  5076  	b = c.GetPacket()
  5077  	defer b.Release()
  5078  	checker.IPv4(t, b,
  5079  		checker.TCP(
  5080  			checker.DstPort(context.TestPort),
  5081  			checker.TCPSeqNum(uint32(c.IRS)+2),
  5082  			checker.TCPAckNum(uint32(iss)+uint32(len(data))+1),
  5083  			checker.TCPFlags(header.TCPFlagAck),
  5084  		),
  5085  	)
  5086  
  5087  	// Give the stack the chance to transition to closed state from
  5088  	// TIME_WAIT.
  5089  	time.Sleep(tcpTimeWaitTimeout * 2)
  5090  
  5091  	if got, want := tcp.EndpointState(c.EP.State()), tcp.StateClose; got != want {
  5092  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
  5093  	}
  5094  
  5095  	// Wait for receive to be notified.
  5096  	select {
  5097  	case <-ch:
  5098  	case <-time.After(1 * time.Second):
  5099  		t.Fatalf("Timed out waiting for data to arrive")
  5100  	}
  5101  
  5102  	// Check that peek works.
  5103  	var peekBuf bytes.Buffer
  5104  	res, err := c.EP.Read(&peekBuf, tcpip.ReadOptions{Peek: true})
  5105  	if err != nil {
  5106  		t.Fatalf("Peek failed: %s", err)
  5107  	}
  5108  
  5109  	if got, want := res.Count, len(data); got != want {
  5110  		t.Fatalf("res.Count = %d, want %d", got, want)
  5111  	}
  5112  	if !bytes.Equal(data, peekBuf.Bytes()) {
  5113  		t.Fatalf("got data = %v, want = %v", peekBuf.Bytes(), data)
  5114  	}
  5115  
  5116  	// Receive data.
  5117  	v := ept.CheckRead(t)
  5118  	if !bytes.Equal(data, v) {
  5119  		t.Fatalf("got data = %v, want = %v", v, data)
  5120  	}
  5121  
  5122  	// Now that we drained the queue, check that functions fail with the
  5123  	// right error code.
  5124  	ept.CheckReadError(t, &tcpip.ErrClosedForReceive{})
  5125  	var buf bytes.Buffer
  5126  	{
  5127  		_, err := c.EP.Read(&buf, tcpip.ReadOptions{Peek: true})
  5128  		if d := cmp.Diff(&tcpip.ErrClosedForReceive{}, err); d != "" {
  5129  			t.Fatalf("c.EP.Read(_, {Peek: true}) mismatch (-want +got):\n%s", d)
  5130  		}
  5131  	}
  5132  }
  5133  
  5134  func TestReusePort(t *testing.T) {
  5135  	// This test ensures that ports are immediately available for reuse
  5136  	// after Close on the endpoints using them returns.
  5137  	c := context.New(t, e2e.DefaultMTU)
  5138  	defer c.Cleanup()
  5139  
  5140  	// First case, just an endpoint that was bound.
  5141  	var err tcpip.Error
  5142  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  5143  	if err != nil {
  5144  		t.Fatalf("NewEndpoint failed; %s", err)
  5145  	}
  5146  	c.EP.SocketOptions().SetReuseAddress(true)
  5147  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  5148  		t.Fatalf("Bind failed: %s", err)
  5149  	}
  5150  
  5151  	c.EP.Close()
  5152  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  5153  	if err != nil {
  5154  		t.Fatalf("NewEndpoint failed; %s", err)
  5155  	}
  5156  	c.EP.SocketOptions().SetReuseAddress(true)
  5157  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  5158  		t.Fatalf("Bind failed: %s", err)
  5159  	}
  5160  	c.EP.Close()
  5161  
  5162  	// Second case, an endpoint that was bound and is connecting..
  5163  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  5164  	if err != nil {
  5165  		t.Fatalf("NewEndpoint failed; %s", err)
  5166  	}
  5167  	c.EP.SocketOptions().SetReuseAddress(true)
  5168  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  5169  		t.Fatalf("Bind failed: %s", err)
  5170  	}
  5171  	{
  5172  		err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort})
  5173  		if d := cmp.Diff(&tcpip.ErrConnectStarted{}, err); d != "" {
  5174  			t.Fatalf("c.EP.Connect(...) mismatch (-want +got):\n%s", d)
  5175  		}
  5176  	}
  5177  	c.EP.Close()
  5178  
  5179  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  5180  	if err != nil {
  5181  		t.Fatalf("NewEndpoint failed; %s", err)
  5182  	}
  5183  	c.EP.SocketOptions().SetReuseAddress(true)
  5184  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  5185  		t.Fatalf("Bind failed: %s", err)
  5186  	}
  5187  	c.EP.Close()
  5188  
  5189  	// Third case, an endpoint that was bound and is listening.
  5190  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  5191  	if err != nil {
  5192  		t.Fatalf("NewEndpoint failed; %s", err)
  5193  	}
  5194  	c.EP.SocketOptions().SetReuseAddress(true)
  5195  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  5196  		t.Fatalf("Bind failed: %s", err)
  5197  	}
  5198  	if err := c.EP.Listen(10); err != nil {
  5199  		t.Fatalf("Listen failed: %s", err)
  5200  	}
  5201  	c.EP.Close()
  5202  
  5203  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  5204  	if err != nil {
  5205  		t.Fatalf("NewEndpoint failed; %s", err)
  5206  	}
  5207  	c.EP.SocketOptions().SetReuseAddress(true)
  5208  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  5209  		t.Fatalf("Bind failed: %s", err)
  5210  	}
  5211  	if err := c.EP.Listen(10); err != nil {
  5212  		t.Fatalf("Listen failed: %s", err)
  5213  	}
  5214  }
  5215  
  5216  func TestTimeWaitAssassination(t *testing.T) {
  5217  	var wg sync.WaitGroup
  5218  	defer wg.Wait()
  5219  	// We need to run this test lots of times because it triggers a very rare race
  5220  	// condition in segment processing.
  5221  	initalTestPort := 1024
  5222  	testRuns := 25
  5223  	for port := initalTestPort; port < initalTestPort+testRuns; port++ {
  5224  		wg.Add(1)
  5225  		go func(port uint16) {
  5226  			defer wg.Done()
  5227  			c := context.New(t, e2e.DefaultMTU)
  5228  			defer c.Cleanup()
  5229  
  5230  			twReuse := tcpip.TCPTimeWaitReuseOption(tcpip.TCPTimeWaitReuseGlobal)
  5231  			if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &twReuse); err != nil {
  5232  				t.Errorf("s.TransportProtocolOption(%v, %v) = %v", tcp.ProtocolNumber, &twReuse, err)
  5233  			}
  5234  
  5235  			if err := c.Stack().SetPortRange(port, port); err != nil {
  5236  				t.Errorf("got s.SetPortRange(%d, %d) = %s, want = nil", port, port, err)
  5237  			}
  5238  
  5239  			iss := seqnum.Value(context.TestInitialSequenceNumber)
  5240  			c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1)
  5241  			c.EP.Close()
  5242  
  5243  			v := c.GetPacket()
  5244  			defer v.Release()
  5245  			checker.IPv4(t, v, checker.TCP(
  5246  				checker.SrcPort(port),
  5247  				checker.DstPort(context.TestPort),
  5248  				checker.TCPSeqNum(uint32(c.IRS+1)),
  5249  				checker.TCPAckNum(uint32(iss)+1),
  5250  				checker.TCPFlags(header.TCPFlagFin|header.TCPFlagAck)))
  5251  
  5252  			finHeaders := &context.Headers{
  5253  				SrcPort: context.TestPort,
  5254  				DstPort: port,
  5255  				Flags:   header.TCPFlagAck | header.TCPFlagFin,
  5256  				SeqNum:  iss + 1,
  5257  				AckNum:  c.IRS + 2,
  5258  			}
  5259  
  5260  			c.SendPacket(nil, finHeaders)
  5261  
  5262  			// c.EP is in TIME_WAIT. We must allow for a second to pass before the
  5263  			// new endpoint is allowed to take over the old endpoint's binding.
  5264  			time.Sleep(time.Second)
  5265  
  5266  			seq := iss + 1
  5267  			ack := c.IRS + 2
  5268  
  5269  			var wg sync.WaitGroup
  5270  			defer wg.Wait()
  5271  
  5272  			wg.Add(1)
  5273  			go func() {
  5274  				defer wg.Done()
  5275  				// The new endpoint will take over the binding.
  5276  				c.Create(-1)
  5277  				timeout := time.After(5 * time.Second)
  5278  			connect:
  5279  				for {
  5280  					select {
  5281  					case <-timeout:
  5282  						break connect
  5283  					default:
  5284  						err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort})
  5285  						// It can take some extra time for the port to be available.
  5286  						if _, ok := err.(*tcpip.ErrNoPortAvailable); ok {
  5287  							continue connect
  5288  						}
  5289  						if _, ok := err.(*tcpip.ErrConnectStarted); !ok {
  5290  							t.Errorf("Unexpected return value from Connect: %v", err)
  5291  						}
  5292  						break connect
  5293  					}
  5294  				}
  5295  			}()
  5296  
  5297  			// If the new endpoint does not properly transition to connecting before
  5298  			// taking over the port reservation, sending acks will cause the processor
  5299  			// to panic 1-5% of the time.
  5300  			for i := 0; i < 5; i++ {
  5301  				wg.Add(1)
  5302  				go func() {
  5303  					defer wg.Done()
  5304  					c.SendPacket(nil, &context.Headers{
  5305  						SrcPort: context.TestPort,
  5306  						DstPort: port,
  5307  						Flags:   header.TCPFlagAck,
  5308  						SeqNum:  seq,
  5309  						AckNum:  ack,
  5310  					})
  5311  				}()
  5312  			}
  5313  		}(uint16(port))
  5314  	}
  5315  }
  5316  
  5317  func checkRecvBufferSize(t *testing.T, ep tcpip.Endpoint, v int) {
  5318  	t.Helper()
  5319  
  5320  	s := ep.SocketOptions().GetReceiveBufferSize()
  5321  	if int(s) != v {
  5322  		t.Fatalf("got receive buffer size = %d, want = %d", s, v)
  5323  	}
  5324  }
  5325  
  5326  func checkSendBufferSize(t *testing.T, ep tcpip.Endpoint, v int) {
  5327  	t.Helper()
  5328  
  5329  	if s := ep.SocketOptions().GetSendBufferSize(); int(s) != v {
  5330  		t.Fatalf("got send buffer size = %d, want = %d", s, v)
  5331  	}
  5332  }
  5333  
  5334  func TestDefaultBufferSizes(t *testing.T) {
  5335  	s := stack.New(stack.Options{
  5336  		NetworkProtocols:   []stack.NetworkProtocolFactory{ipv4.NewProtocol},
  5337  		TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol},
  5338  	})
  5339  	defer s.Destroy()
  5340  
  5341  	// Check the default values.
  5342  	ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  5343  	if err != nil {
  5344  		t.Fatalf("NewEndpoint failed; %s", err)
  5345  	}
  5346  	defer func() {
  5347  		if ep != nil {
  5348  			ep.Close()
  5349  		}
  5350  	}()
  5351  
  5352  	checkSendBufferSize(t, ep, tcp.DefaultSendBufferSize)
  5353  	checkRecvBufferSize(t, ep, tcp.DefaultReceiveBufferSize)
  5354  
  5355  	// Change the default send buffer size.
  5356  	{
  5357  		opt := tcpip.TCPSendBufferSizeRangeOption{
  5358  			Min:     1,
  5359  			Default: tcp.DefaultSendBufferSize * 2,
  5360  			Max:     tcp.DefaultSendBufferSize * 20,
  5361  		}
  5362  		if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  5363  			t.Fatalf("SetTransportProtocolOption(%d, &%#v): %s", tcp.ProtocolNumber, opt, err)
  5364  		}
  5365  	}
  5366  
  5367  	ep.Close()
  5368  	ep, err = s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  5369  	if err != nil {
  5370  		t.Fatalf("NewEndpoint failed; %s", err)
  5371  	}
  5372  
  5373  	checkSendBufferSize(t, ep, tcp.DefaultSendBufferSize*2)
  5374  	checkRecvBufferSize(t, ep, tcp.DefaultReceiveBufferSize)
  5375  
  5376  	// Change the default receive buffer size.
  5377  	{
  5378  		opt := tcpip.TCPReceiveBufferSizeRangeOption{
  5379  			Min:     1,
  5380  			Default: tcp.DefaultReceiveBufferSize * 3,
  5381  			Max:     tcp.DefaultReceiveBufferSize * 30,
  5382  		}
  5383  		if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  5384  			t.Fatalf("SetTransportProtocolOption(%d, &%#v): %s", tcp.ProtocolNumber, opt, err)
  5385  		}
  5386  	}
  5387  
  5388  	ep.Close()
  5389  	ep, err = s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  5390  	if err != nil {
  5391  		t.Fatalf("NewEndpoint failed; %s", err)
  5392  	}
  5393  
  5394  	checkSendBufferSize(t, ep, tcp.DefaultSendBufferSize*2)
  5395  	checkRecvBufferSize(t, ep, tcp.DefaultReceiveBufferSize*3)
  5396  }
  5397  
  5398  func TestBindToDeviceOption(t *testing.T) {
  5399  	s := stack.New(stack.Options{
  5400  		NetworkProtocols:   []stack.NetworkProtocolFactory{ipv4.NewProtocol},
  5401  		TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol}})
  5402  
  5403  	defer s.Destroy()
  5404  	ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  5405  	if err != nil {
  5406  		t.Fatalf("NewEndpoint failed; %s", err)
  5407  	}
  5408  	defer ep.Close()
  5409  
  5410  	if err := s.CreateNIC(321, loopback.New()); err != nil {
  5411  		t.Errorf("CreateNIC failed: %s", err)
  5412  	}
  5413  
  5414  	// nicIDPtr is used instead of taking the address of NICID literals, which is
  5415  	// a compiler error.
  5416  	nicIDPtr := func(s tcpip.NICID) *tcpip.NICID {
  5417  		return &s
  5418  	}
  5419  
  5420  	testActions := []struct {
  5421  		name                 string
  5422  		setBindToDevice      *tcpip.NICID
  5423  		setBindToDeviceError tcpip.Error
  5424  		getBindToDevice      int32
  5425  	}{
  5426  		{"GetDefaultValue", nil, nil, 0},
  5427  		{"BindToNonExistent", nicIDPtr(999), &tcpip.ErrUnknownDevice{}, 0},
  5428  		{"BindToExistent", nicIDPtr(321), nil, 321},
  5429  		{"UnbindToDevice", nicIDPtr(0), nil, 0},
  5430  	}
  5431  	for _, testAction := range testActions {
  5432  		t.Run(testAction.name, func(t *testing.T) {
  5433  			if testAction.setBindToDevice != nil {
  5434  				bindToDevice := int32(*testAction.setBindToDevice)
  5435  				if gotErr, wantErr := ep.SocketOptions().SetBindToDevice(bindToDevice), testAction.setBindToDeviceError; gotErr != wantErr {
  5436  					t.Errorf("got SetSockOpt(&%T(%d)) = %s, want = %s", bindToDevice, bindToDevice, gotErr, wantErr)
  5437  				}
  5438  			}
  5439  			bindToDevice := ep.SocketOptions().GetBindToDevice()
  5440  			if bindToDevice != testAction.getBindToDevice {
  5441  				t.Errorf("got bindToDevice = %d, want %d", bindToDevice, testAction.getBindToDevice)
  5442  			}
  5443  		})
  5444  	}
  5445  }
  5446  
  5447  func makeStack() (*stack.Stack, tcpip.Error) {
  5448  	s := stack.New(stack.Options{
  5449  		NetworkProtocols: []stack.NetworkProtocolFactory{
  5450  			ipv4.NewProtocol,
  5451  			ipv6.NewProtocol,
  5452  		},
  5453  		TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol},
  5454  	})
  5455  
  5456  	id := loopback.New()
  5457  	if testing.Verbose() {
  5458  		id = sniffer.New(id)
  5459  	}
  5460  
  5461  	if err := s.CreateNIC(1, id); err != nil {
  5462  		return nil, err
  5463  	}
  5464  
  5465  	for _, ct := range []struct {
  5466  		number         tcpip.NetworkProtocolNumber
  5467  		addrWithPrefix tcpip.AddressWithPrefix
  5468  	}{
  5469  		{ipv4.ProtocolNumber, context.StackAddrWithPrefix},
  5470  		{ipv6.ProtocolNumber, context.StackV6AddrWithPrefix},
  5471  	} {
  5472  		protocolAddr := tcpip.ProtocolAddress{
  5473  			Protocol:          ct.number,
  5474  			AddressWithPrefix: ct.addrWithPrefix,
  5475  		}
  5476  		if err := s.AddProtocolAddress(1, protocolAddr, stack.AddressProperties{}); err != nil {
  5477  			return nil, err
  5478  		}
  5479  	}
  5480  
  5481  	s.SetRouteTable([]tcpip.Route{
  5482  		{
  5483  			Destination: header.IPv4EmptySubnet,
  5484  			NIC:         1,
  5485  		},
  5486  		{
  5487  			Destination: header.IPv6EmptySubnet,
  5488  			NIC:         1,
  5489  		},
  5490  	})
  5491  
  5492  	return s, nil
  5493  }
  5494  
  5495  func TestSelfConnect(t *testing.T) {
  5496  	// This test ensures that intentional self-connects work. In particular,
  5497  	// it checks that if an endpoint binds to say 127.0.0.1:1000 then
  5498  	// connects to 127.0.0.1:1000, then it will be connected to itself, and
  5499  	// is able to send and receive data through the same endpoint.
  5500  	s, err := makeStack()
  5501  	if err != nil {
  5502  		t.Fatal(err)
  5503  	}
  5504  	defer s.Destroy()
  5505  
  5506  	var wq waiter.Queue
  5507  	ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq)
  5508  	if err != nil {
  5509  		t.Fatalf("NewEndpoint failed: %s", err)
  5510  	}
  5511  	defer ep.Close()
  5512  
  5513  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  5514  		t.Fatalf("Bind failed: %s", err)
  5515  	}
  5516  
  5517  	// Register for notification, then start connection attempt.
  5518  	waitEntry, notifyCh := waiter.NewChannelEntry(waiter.WritableEvents)
  5519  	wq.EventRegister(&waitEntry)
  5520  	defer wq.EventUnregister(&waitEntry)
  5521  
  5522  	{
  5523  		err := ep.Connect(tcpip.FullAddress{Addr: context.StackAddr, Port: context.StackPort})
  5524  		if d := cmp.Diff(&tcpip.ErrConnectStarted{}, err); d != "" {
  5525  			t.Fatalf("ep.Connect(...) mismatch (-want +got):\n%s", d)
  5526  		}
  5527  	}
  5528  
  5529  	<-notifyCh
  5530  	if err := ep.LastError(); err != nil {
  5531  		t.Fatalf("Connect failed: %s", err)
  5532  	}
  5533  
  5534  	// Write something.
  5535  	data := []byte{1, 2, 3}
  5536  	var r bytes.Reader
  5537  	r.Reset(data)
  5538  	if _, err := ep.Write(&r, tcpip.WriteOptions{}); err != nil {
  5539  		t.Fatalf("Write failed: %s", err)
  5540  	}
  5541  
  5542  	// Read back what was written.
  5543  	wq.EventUnregister(&waitEntry)
  5544  	waitEntry, notifyCh = waiter.NewChannelEntry(waiter.ReadableEvents)
  5545  	wq.EventRegister(&waitEntry)
  5546  	ept := endpointTester{ep}
  5547  	rd := ept.CheckReadFull(t, len(data), notifyCh, 5*time.Second)
  5548  
  5549  	if !bytes.Equal(data, rd) {
  5550  		t.Fatalf("got data = %v, want = %v", rd, data)
  5551  	}
  5552  }
  5553  
  5554  func TestConnectAvoidsBoundPorts(t *testing.T) {
  5555  	addressTypes := func(t *testing.T, network string) []string {
  5556  		switch network {
  5557  		case "ipv4":
  5558  			return []string{"v4"}
  5559  		case "ipv6":
  5560  			return []string{"v6"}
  5561  		case "dual":
  5562  			return []string{"v6", "mapped"}
  5563  		default:
  5564  			t.Fatalf("unknown network: '%s'", network)
  5565  		}
  5566  
  5567  		panic("unreachable")
  5568  	}
  5569  
  5570  	address := func(t *testing.T, addressType string, isAny bool) tcpip.Address {
  5571  		switch addressType {
  5572  		case "v4":
  5573  			if isAny {
  5574  				return tcpip.Address{}
  5575  			}
  5576  			return context.StackAddr
  5577  		case "v6":
  5578  			if isAny {
  5579  				return tcpip.Address{}
  5580  			}
  5581  			return context.StackV6Addr
  5582  		case "mapped":
  5583  			if isAny {
  5584  				return context.V4MappedWildcardAddr
  5585  			}
  5586  			return context.StackV4MappedAddr
  5587  		default:
  5588  			t.Fatalf("unknown address type: '%s'", addressType)
  5589  		}
  5590  
  5591  		panic("unreachable")
  5592  	}
  5593  	// This test ensures that Endpoint.Connect doesn't select already-bound ports.
  5594  	networks := []string{"ipv4", "ipv6", "dual"}
  5595  	for _, exhaustedNetwork := range networks {
  5596  		t.Run(fmt.Sprintf("exhaustedNetwork=%s", exhaustedNetwork), func(t *testing.T) {
  5597  			for _, exhaustedAddressType := range addressTypes(t, exhaustedNetwork) {
  5598  				t.Run(fmt.Sprintf("exhaustedAddressType=%s", exhaustedAddressType), func(t *testing.T) {
  5599  					for _, isAny := range []bool{false, true} {
  5600  						t.Run(fmt.Sprintf("isAny=%t", isAny), func(t *testing.T) {
  5601  							for _, candidateNetwork := range networks {
  5602  								t.Run(fmt.Sprintf("candidateNetwork=%s", candidateNetwork), func(t *testing.T) {
  5603  									for _, candidateAddressType := range addressTypes(t, candidateNetwork) {
  5604  										t.Run(fmt.Sprintf("candidateAddressType=%s", candidateAddressType), func(t *testing.T) {
  5605  											s, err := makeStack()
  5606  											if err != nil {
  5607  												t.Fatal(err)
  5608  											}
  5609  											defer s.Destroy()
  5610  
  5611  											var wq waiter.Queue
  5612  											var eps []tcpip.Endpoint
  5613  											defer func() {
  5614  												for _, ep := range eps {
  5615  													ep.Close()
  5616  												}
  5617  											}()
  5618  											makeEP := func(network string) tcpip.Endpoint {
  5619  												var networkProtocolNumber tcpip.NetworkProtocolNumber
  5620  												switch network {
  5621  												case "ipv4":
  5622  													networkProtocolNumber = ipv4.ProtocolNumber
  5623  												case "ipv6", "dual":
  5624  													networkProtocolNumber = ipv6.ProtocolNumber
  5625  												default:
  5626  													t.Fatalf("unknown network: '%s'", network)
  5627  												}
  5628  												ep, err := s.NewEndpoint(tcp.ProtocolNumber, networkProtocolNumber, &wq)
  5629  												if err != nil {
  5630  													t.Fatalf("NewEndpoint failed: %s", err)
  5631  												}
  5632  												eps = append(eps, ep)
  5633  												switch network {
  5634  												case "ipv4":
  5635  												case "ipv6":
  5636  													ep.SocketOptions().SetV6Only(true)
  5637  												case "dual":
  5638  													ep.SocketOptions().SetV6Only(false)
  5639  												default:
  5640  													t.Fatalf("unknown network: '%s'", network)
  5641  												}
  5642  												return ep
  5643  											}
  5644  
  5645  											var v4reserved, v6reserved bool
  5646  											switch exhaustedAddressType {
  5647  											case "v4", "mapped":
  5648  												v4reserved = true
  5649  											case "v6":
  5650  												v6reserved = true
  5651  												// Dual stack sockets bound to v6 any reserve on v4 as
  5652  												// well.
  5653  												if isAny {
  5654  													switch exhaustedNetwork {
  5655  													case "ipv6":
  5656  													case "dual":
  5657  														v4reserved = true
  5658  													default:
  5659  														t.Fatalf("unknown address type: '%s'", exhaustedNetwork)
  5660  													}
  5661  												}
  5662  											default:
  5663  												t.Fatalf("unknown address type: '%s'", exhaustedAddressType)
  5664  											}
  5665  											var collides bool
  5666  											switch candidateAddressType {
  5667  											case "v4", "mapped":
  5668  												collides = v4reserved
  5669  											case "v6":
  5670  												collides = v6reserved
  5671  											default:
  5672  												t.Fatalf("unknown address type: '%s'", candidateAddressType)
  5673  											}
  5674  
  5675  											const (
  5676  												start = 16000
  5677  												end   = 16050
  5678  											)
  5679  											if err := s.SetPortRange(start, end); err != nil {
  5680  												t.Fatalf("got s.SetPortRange(%d, %d) = %s, want = nil", start, end, err)
  5681  											}
  5682  											for i := start; i <= end; i++ {
  5683  												if err := makeEP(exhaustedNetwork).Bind(tcpip.FullAddress{Addr: address(t, exhaustedAddressType, isAny), Port: uint16(i)}); err != nil {
  5684  													t.Fatalf("Bind(%d) failed: %s", i, err)
  5685  												}
  5686  											}
  5687  											var want tcpip.Error = &tcpip.ErrConnectStarted{}
  5688  											if collides {
  5689  												want = &tcpip.ErrNoPortAvailable{}
  5690  											}
  5691  											if err := makeEP(candidateNetwork).Connect(tcpip.FullAddress{Addr: address(t, candidateAddressType, false), Port: 31337}); err != want {
  5692  												t.Fatalf("got ep.Connect(..) = %s, want = %s", err, want)
  5693  											}
  5694  										})
  5695  									}
  5696  								})
  5697  							}
  5698  						})
  5699  					}
  5700  				})
  5701  			}
  5702  		})
  5703  	}
  5704  }
  5705  
  5706  func TestPathMTUDiscovery(t *testing.T) {
  5707  	// This test verifies the stack retransmits packets after it receives an
  5708  	// ICMP packet indicating that the path MTU has been exceeded.
  5709  	c := context.New(t, 1500)
  5710  	defer c.Cleanup()
  5711  
  5712  	// Create new connection with MSS of 1460.
  5713  	const maxPayload = 1500 - header.TCPMinimumSize - header.IPv4MinimumSize
  5714  	c.CreateConnectedWithRawOptions(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */, []byte{
  5715  		header.TCPOptionMSS, 4, byte(maxPayload / 256), byte(maxPayload % 256),
  5716  	})
  5717  
  5718  	// Send 3200 bytes of data.
  5719  	const writeSize = 3200
  5720  	data := make([]byte, writeSize)
  5721  	for i := range data {
  5722  		data[i] = byte(i)
  5723  	}
  5724  	var r bytes.Reader
  5725  	r.Reset(data)
  5726  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  5727  		t.Fatalf("Write failed: %s", err)
  5728  	}
  5729  
  5730  	receivePackets := func(c *context.Context, sizes []int, which int, seqNum uint32) *buffer.View {
  5731  		var ret *buffer.View
  5732  		iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  5733  		for i, size := range sizes {
  5734  			p := c.GetPacket()
  5735  			if i == which {
  5736  				ret = p
  5737  			} else {
  5738  				defer p.Release()
  5739  			}
  5740  			checker.IPv4(t, p,
  5741  				checker.PayloadLen(size+header.TCPMinimumSize),
  5742  				checker.FragmentFlags(header.IPv4FlagDontFragment),
  5743  				checker.TCP(
  5744  					checker.DstPort(context.TestPort),
  5745  					checker.TCPSeqNum(seqNum),
  5746  					checker.TCPAckNum(uint32(iss)),
  5747  					checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  5748  				),
  5749  			)
  5750  			seqNum += uint32(size)
  5751  		}
  5752  		return ret
  5753  	}
  5754  
  5755  	// Receive three packets.
  5756  	sizes := []int{maxPayload, maxPayload, writeSize - 2*maxPayload}
  5757  	first := receivePackets(c, sizes, 0, uint32(c.IRS)+1)
  5758  	defer first.Release()
  5759  
  5760  	// Send "packet too big" messages back to netstack.
  5761  	const newMTU = 1200
  5762  	const newMaxPayload = newMTU - header.IPv4MinimumSize - header.TCPMinimumSize
  5763  	mtu := buffer.NewViewWithData([]byte{0, 0, newMTU / 256, newMTU % 256})
  5764  	defer mtu.Release()
  5765  	c.SendICMPPacket(header.ICMPv4DstUnreachable, header.ICMPv4FragmentationNeeded, mtu, first, newMTU)
  5766  
  5767  	// See retransmitted packets. None exceeding the new max.
  5768  	sizes = []int{newMaxPayload, maxPayload - newMaxPayload, newMaxPayload, maxPayload - newMaxPayload, writeSize - 2*maxPayload}
  5769  	receivePackets(c, sizes, -1, uint32(c.IRS)+1)
  5770  }
  5771  
  5772  func TestTCPEndpointProbe(t *testing.T) {
  5773  	c := context.New(t, 1500)
  5774  	defer c.Cleanup()
  5775  
  5776  	invoked := make(chan struct{})
  5777  	c.Stack().AddTCPProbe(func(state *stack.TCPEndpointState) {
  5778  		// Validate that the endpoint ID is what we expect.
  5779  		//
  5780  		// We don't do an extensive validation of every field but a
  5781  		// basic sanity test.
  5782  		if got, want := state.ID.LocalAddress, tcpip.Address(context.StackAddr); got != want {
  5783  			t.Fatalf("got LocalAddress: %q, want: %q", got, want)
  5784  		}
  5785  		if got, want := state.ID.LocalPort, c.Port; got != want {
  5786  			t.Fatalf("got LocalPort: %d, want: %d", got, want)
  5787  		}
  5788  		if got, want := state.ID.RemoteAddress, tcpip.Address(context.TestAddr); got != want {
  5789  			t.Fatalf("got RemoteAddress: %q, want: %q", got, want)
  5790  		}
  5791  		if got, want := state.ID.RemotePort, uint16(context.TestPort); got != want {
  5792  			t.Fatalf("got RemotePort: %d, want: %d", got, want)
  5793  		}
  5794  
  5795  		invoked <- struct{}{}
  5796  	})
  5797  
  5798  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  5799  
  5800  	data := []byte{1, 2, 3}
  5801  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  5802  	c.SendPacket(data, &context.Headers{
  5803  		SrcPort: context.TestPort,
  5804  		DstPort: c.Port,
  5805  		Flags:   header.TCPFlagAck,
  5806  		SeqNum:  iss,
  5807  		AckNum:  c.IRS.Add(1),
  5808  		RcvWnd:  30000,
  5809  	})
  5810  
  5811  	select {
  5812  	case <-invoked:
  5813  	case <-time.After(100 * time.Millisecond):
  5814  		t.Fatalf("TCP Probe function was not called")
  5815  	}
  5816  }
  5817  
  5818  func TestStackSetCongestionControl(t *testing.T) {
  5819  	testCases := []struct {
  5820  		cc  tcpip.CongestionControlOption
  5821  		err tcpip.Error
  5822  	}{
  5823  		{"reno", nil},
  5824  		{"cubic", nil},
  5825  		{"blahblah", &tcpip.ErrNoSuchFile{}},
  5826  	}
  5827  
  5828  	for _, tc := range testCases {
  5829  		t.Run(fmt.Sprintf("SetTransportProtocolOption(.., %v)", tc.cc), func(t *testing.T) {
  5830  			c := context.New(t, 1500)
  5831  			defer c.Cleanup()
  5832  
  5833  			s := c.Stack()
  5834  
  5835  			var oldCC tcpip.CongestionControlOption
  5836  			if err := s.TransportProtocolOption(tcp.ProtocolNumber, &oldCC); err != nil {
  5837  				t.Fatalf("s.TransportProtocolOption(%v, %v) = %s", tcp.ProtocolNumber, &oldCC, err)
  5838  			}
  5839  
  5840  			if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &tc.cc); err != tc.err {
  5841  				t.Fatalf("s.SetTransportProtocolOption(%d, &%T(%s)) = %s, want = %s", tcp.ProtocolNumber, tc.cc, tc.cc, err, tc.err)
  5842  			}
  5843  
  5844  			var cc tcpip.CongestionControlOption
  5845  			if err := s.TransportProtocolOption(tcp.ProtocolNumber, &cc); err != nil {
  5846  				t.Fatalf("s.TransportProtocolOption(%v, %v) = %v", tcp.ProtocolNumber, &cc, err)
  5847  			}
  5848  
  5849  			got, want := cc, oldCC
  5850  			// If SetTransportProtocolOption is expected to succeed
  5851  			// then the returned value for congestion control should
  5852  			// match the one specified in the
  5853  			// SetTransportProtocolOption call above, else it should
  5854  			// be what it was before the call to
  5855  			// SetTransportProtocolOption.
  5856  			if tc.err == nil {
  5857  				want = tc.cc
  5858  			}
  5859  			if got != want {
  5860  				t.Fatalf("got congestion control: %v, want: %v", got, want)
  5861  			}
  5862  		})
  5863  	}
  5864  }
  5865  
  5866  func TestStackAvailableCongestionControl(t *testing.T) {
  5867  	c := context.New(t, 1500)
  5868  	defer c.Cleanup()
  5869  
  5870  	s := c.Stack()
  5871  
  5872  	// Query permitted congestion control algorithms.
  5873  	var aCC tcpip.TCPAvailableCongestionControlOption
  5874  	if err := s.TransportProtocolOption(tcp.ProtocolNumber, &aCC); err != nil {
  5875  		t.Fatalf("s.TransportProtocolOption(%v, %v) = %v", tcp.ProtocolNumber, &aCC, err)
  5876  	}
  5877  	if got, want := aCC, tcpip.TCPAvailableCongestionControlOption("reno cubic"); got != want {
  5878  		t.Fatalf("got tcpip.TCPAvailableCongestionControlOption: %v, want: %v", got, want)
  5879  	}
  5880  }
  5881  
  5882  func TestStackSetAvailableCongestionControl(t *testing.T) {
  5883  	c := context.New(t, 1500)
  5884  	defer c.Cleanup()
  5885  
  5886  	s := c.Stack()
  5887  
  5888  	// Setting AvailableCongestionControlOption should fail.
  5889  	aCC := tcpip.TCPAvailableCongestionControlOption("xyz")
  5890  	if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &aCC); err == nil {
  5891  		t.Fatalf("s.SetTransportProtocolOption(%d, &%T(%s)) = nil, want non-nil", tcp.ProtocolNumber, aCC, aCC)
  5892  	}
  5893  
  5894  	// Verify that we still get the expected list of congestion control options.
  5895  	var cc tcpip.TCPAvailableCongestionControlOption
  5896  	if err := s.TransportProtocolOption(tcp.ProtocolNumber, &cc); err != nil {
  5897  		t.Fatalf("s.TransportProtocolOptio(%d, &%T(%s)): %s", tcp.ProtocolNumber, cc, cc, err)
  5898  	}
  5899  	if got, want := cc, tcpip.TCPAvailableCongestionControlOption("reno cubic"); got != want {
  5900  		t.Fatalf("got tcpip.TCPAvailableCongestionControlOption = %s, want = %s", got, want)
  5901  	}
  5902  }
  5903  
  5904  func TestEndpointSetCongestionControl(t *testing.T) {
  5905  	testCases := []struct {
  5906  		cc  tcpip.CongestionControlOption
  5907  		err tcpip.Error
  5908  	}{
  5909  		{"reno", nil},
  5910  		{"cubic", nil},
  5911  		{"blahblah", &tcpip.ErrNoSuchFile{}},
  5912  	}
  5913  
  5914  	for _, connected := range []bool{false, true} {
  5915  		for _, tc := range testCases {
  5916  			t.Run(fmt.Sprintf("SetSockOpt(.., %v) w/ connected = %v", tc.cc, connected), func(t *testing.T) {
  5917  				c := context.New(t, 1500)
  5918  				defer c.Cleanup()
  5919  
  5920  				// Create TCP endpoint.
  5921  				var err tcpip.Error
  5922  				c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  5923  				if err != nil {
  5924  					t.Fatalf("NewEndpoint failed: %s", err)
  5925  				}
  5926  
  5927  				var oldCC tcpip.CongestionControlOption
  5928  				if err := c.EP.GetSockOpt(&oldCC); err != nil {
  5929  					t.Fatalf("c.EP.GetSockOpt(&%T) = %s", oldCC, err)
  5930  				}
  5931  
  5932  				if connected {
  5933  					c.Connect(context.TestInitialSequenceNumber, 32768 /* rcvWnd */, nil)
  5934  				}
  5935  
  5936  				if err := c.EP.SetSockOpt(&tc.cc); err != tc.err {
  5937  					t.Fatalf("got c.EP.SetSockOpt(&%#v) = %s, want %s", tc.cc, err, tc.err)
  5938  				}
  5939  
  5940  				var cc tcpip.CongestionControlOption
  5941  				if err := c.EP.GetSockOpt(&cc); err != nil {
  5942  					t.Fatalf("c.EP.GetSockOpt(&%T): %s", cc, err)
  5943  				}
  5944  
  5945  				got, want := cc, oldCC
  5946  				// If SetSockOpt is expected to succeed then the
  5947  				// returned value for congestion control should match
  5948  				// the one specified in the SetSockOpt above, else it
  5949  				// should be what it was before the call to SetSockOpt.
  5950  				if tc.err == nil {
  5951  					want = tc.cc
  5952  				}
  5953  				if got != want {
  5954  					t.Fatalf("got congestion control = %+v, want = %+v", got, want)
  5955  				}
  5956  			})
  5957  		}
  5958  	}
  5959  }
  5960  
  5961  func TestKeepalive(t *testing.T) {
  5962  	c := context.New(t, e2e.DefaultMTU)
  5963  	defer c.Cleanup()
  5964  
  5965  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  5966  
  5967  	const keepAliveIdle = 100 * time.Millisecond
  5968  	const keepAliveInterval = 3 * time.Second
  5969  	keepAliveIdleOpt := tcpip.KeepaliveIdleOption(keepAliveIdle)
  5970  	if err := c.EP.SetSockOpt(&keepAliveIdleOpt); err != nil {
  5971  		t.Fatalf("c.EP.SetSockOpt(&%T(%s)): %s", keepAliveIdleOpt, keepAliveIdle, err)
  5972  	}
  5973  	keepAliveIntervalOpt := tcpip.KeepaliveIntervalOption(keepAliveInterval)
  5974  	if err := c.EP.SetSockOpt(&keepAliveIntervalOpt); err != nil {
  5975  		t.Fatalf("c.EP.SetSockOpt(&%T(%s)): %s", keepAliveIntervalOpt, keepAliveInterval, err)
  5976  	}
  5977  	c.EP.SetSockOptInt(tcpip.KeepaliveCountOption, 5)
  5978  	if err := c.EP.SetSockOptInt(tcpip.KeepaliveCountOption, 5); err != nil {
  5979  		t.Fatalf("c.EP.SetSockOptInt(tcpip.KeepaliveCountOption, 5): %s", err)
  5980  	}
  5981  	c.EP.SocketOptions().SetKeepAlive(true)
  5982  
  5983  	// 5 unacked keepalives are sent. ACK each one, and check that the
  5984  	// connection stays alive after 5.
  5985  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  5986  	for i := 0; i < 10; i++ {
  5987  		b := c.GetPacket()
  5988  		defer b.Release()
  5989  		checker.IPv4(t, b,
  5990  			checker.TCP(
  5991  				checker.DstPort(context.TestPort),
  5992  				checker.TCPSeqNum(uint32(c.IRS)),
  5993  				checker.TCPAckNum(uint32(iss)),
  5994  				checker.TCPFlags(header.TCPFlagAck),
  5995  			),
  5996  		)
  5997  
  5998  		// Acknowledge the keepalive.
  5999  		c.SendPacket(nil, &context.Headers{
  6000  			SrcPort: context.TestPort,
  6001  			DstPort: c.Port,
  6002  			Flags:   header.TCPFlagAck,
  6003  			SeqNum:  iss,
  6004  			AckNum:  c.IRS,
  6005  			RcvWnd:  30000,
  6006  		})
  6007  	}
  6008  
  6009  	// Check that the connection is still alive.
  6010  	ept := endpointTester{c.EP}
  6011  	ept.CheckReadError(t, &tcpip.ErrWouldBlock{})
  6012  
  6013  	// Send some data and wait before ACKing it. Keepalives should be disabled
  6014  	// during this period.
  6015  	view := make([]byte, 3)
  6016  	var r bytes.Reader
  6017  	r.Reset(view)
  6018  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  6019  		t.Fatalf("Write failed: %s", err)
  6020  	}
  6021  
  6022  	next := uint32(c.IRS) + 1
  6023  	b := c.GetPacket()
  6024  	defer b.Release()
  6025  	checker.IPv4(t, b,
  6026  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  6027  		checker.TCP(
  6028  			checker.DstPort(context.TestPort),
  6029  			checker.TCPSeqNum(next),
  6030  			checker.TCPAckNum(uint32(iss)),
  6031  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  6032  		),
  6033  	)
  6034  
  6035  	// Wait for the packet to be retransmitted. Verify that no keepalives
  6036  	// were sent.
  6037  	b = c.GetPacket()
  6038  	defer b.Release()
  6039  	checker.IPv4(t, b,
  6040  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  6041  		checker.TCP(
  6042  			checker.DstPort(context.TestPort),
  6043  			checker.TCPSeqNum(next),
  6044  			checker.TCPAckNum(uint32(iss)),
  6045  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagPsh),
  6046  		),
  6047  	)
  6048  	c.CheckNoPacket("Keepalive packet received while unACKed data is pending")
  6049  
  6050  	next += uint32(len(view))
  6051  
  6052  	// Send ACK. Keepalives should start sending again.
  6053  	c.SendPacket(nil, &context.Headers{
  6054  		SrcPort: context.TestPort,
  6055  		DstPort: c.Port,
  6056  		Flags:   header.TCPFlagAck,
  6057  		SeqNum:  iss,
  6058  		AckNum:  seqnum.Value(next),
  6059  		RcvWnd:  30000,
  6060  	})
  6061  
  6062  	// Now receive 5 keepalives, but don't ACK them. The connection
  6063  	// should be reset after 5.
  6064  	for i := 0; i < 5; i++ {
  6065  		b = c.GetPacket()
  6066  		defer b.Release()
  6067  		checker.IPv4(t, b,
  6068  			checker.TCP(
  6069  				checker.DstPort(context.TestPort),
  6070  				checker.TCPSeqNum(next-1),
  6071  				checker.TCPAckNum(uint32(iss)),
  6072  				checker.TCPFlags(header.TCPFlagAck),
  6073  			),
  6074  		)
  6075  	}
  6076  
  6077  	// Sleep for a little over the KeepAlive interval to make sure
  6078  	// the timer has time to fire after the last ACK and close the
  6079  	// close the socket.
  6080  	time.Sleep(keepAliveInterval + keepAliveInterval/2)
  6081  
  6082  	// The connection should be terminated after 5 unacked keepalives.
  6083  	// Send an ACK to trigger a RST from the stack as the endpoint should
  6084  	// be dead.
  6085  	c.SendPacket(nil, &context.Headers{
  6086  		SrcPort: context.TestPort,
  6087  		DstPort: c.Port,
  6088  		Flags:   header.TCPFlagAck,
  6089  		SeqNum:  iss,
  6090  		AckNum:  seqnum.Value(next),
  6091  		RcvWnd:  30000,
  6092  	})
  6093  
  6094  	v := c.GetPacket()
  6095  	defer v.Release()
  6096  	checker.IPv4(t, v,
  6097  		checker.TCP(checker.DstPort(context.TestPort), checker.TCPSeqNum(next), checker.TCPAckNum(uint32(0)), checker.TCPFlags(header.TCPFlagRst)),
  6098  	)
  6099  
  6100  	if got := c.Stack().Stats().TCP.EstablishedTimedout.Value(); got != 1 {
  6101  		t.Errorf("got c.Stack().Stats().TCP.EstablishedTimedout.Value() = %d, want = 1", got)
  6102  	}
  6103  
  6104  	ept.CheckReadError(t, &tcpip.ErrTimeout{})
  6105  
  6106  	if got := c.Stack().Stats().TCP.CurrentEstablished.Value(); got != 0 {
  6107  		t.Errorf("got stats.TCP.CurrentEstablished.Value() = %d, want = 0", got)
  6108  	}
  6109  	if got := c.Stack().Stats().TCP.CurrentConnected.Value(); got != 0 {
  6110  		t.Errorf("got stats.TCP.CurrentConnected.Value() = %d, want = 0", got)
  6111  	}
  6112  }
  6113  
  6114  func executeHandshake(t *testing.T, c *context.Context, srcPort uint16, synCookieInUse bool) (irs, iss seqnum.Value) {
  6115  	t.Helper()
  6116  
  6117  	// Send a SYN request.
  6118  	options := []byte{header.TCPOptionWS, 3, 0, header.TCPOptionNOP}
  6119  	irs = seqnum.Value(context.TestInitialSequenceNumber)
  6120  	c.SendPacket(nil, &context.Headers{
  6121  		SrcPort: srcPort,
  6122  		DstPort: context.StackPort,
  6123  		Flags:   header.TCPFlagSyn,
  6124  		SeqNum:  irs,
  6125  		RcvWnd:  30000,
  6126  		TCPOpts: options,
  6127  	})
  6128  
  6129  	// Receive the SYN-ACK reply.
  6130  	b := c.GetPacket()
  6131  	defer b.Release()
  6132  	tcpHdr := header.TCP(header.IPv4(b.AsSlice()).Payload())
  6133  	iss = seqnum.Value(tcpHdr.SequenceNumber())
  6134  	tcpCheckers := []checker.TransportChecker{
  6135  		checker.SrcPort(context.StackPort),
  6136  		checker.DstPort(srcPort),
  6137  		checker.TCPFlags(header.TCPFlagAck | header.TCPFlagSyn),
  6138  		checker.TCPAckNum(uint32(irs) + 1),
  6139  	}
  6140  
  6141  	if synCookieInUse {
  6142  		// When cookies are in use window scaling is disabled.
  6143  		tcpCheckers = append(tcpCheckers, checker.TCPSynOptions(header.TCPSynOptions{
  6144  			WS:  -1,
  6145  			MSS: c.MSSWithoutOptions(),
  6146  		}))
  6147  	} else {
  6148  		tcpCheckers = append(tcpCheckers, checker.TCPSynOptions(header.TCPSynOptions{
  6149  			WS:  tcp.FindWndScale(tcp.DefaultReceiveBufferSize),
  6150  			MSS: c.MSSWithoutOptions(),
  6151  		}))
  6152  	}
  6153  
  6154  	checker.IPv4(t, b, checker.TCP(tcpCheckers...))
  6155  
  6156  	// Send ACK.
  6157  	c.SendPacket(nil, &context.Headers{
  6158  		SrcPort: srcPort,
  6159  		DstPort: context.StackPort,
  6160  		Flags:   header.TCPFlagAck,
  6161  		SeqNum:  irs + 1,
  6162  		AckNum:  iss + 1,
  6163  		RcvWnd:  30000,
  6164  	})
  6165  	return irs, iss
  6166  }
  6167  
  6168  func executeV6Handshake(t *testing.T, c *context.Context, srcPort uint16, synCookieInUse bool) (irs, iss seqnum.Value) {
  6169  	t.Helper()
  6170  
  6171  	// Send a SYN request.
  6172  	options := []byte{header.TCPOptionWS, 3, 0, header.TCPOptionNOP}
  6173  	irs = seqnum.Value(context.TestInitialSequenceNumber)
  6174  	c.SendV6Packet(nil, &context.Headers{
  6175  		SrcPort: srcPort,
  6176  		DstPort: context.StackPort,
  6177  		Flags:   header.TCPFlagSyn,
  6178  		SeqNum:  irs,
  6179  		RcvWnd:  30000,
  6180  		TCPOpts: options,
  6181  	})
  6182  
  6183  	// Receive the SYN-ACK reply.
  6184  	b := c.GetV6Packet()
  6185  	defer b.Release()
  6186  	tcpHdr := header.TCP(header.IPv6(b.AsSlice()).Payload())
  6187  	iss = seqnum.Value(tcpHdr.SequenceNumber())
  6188  	tcpCheckers := []checker.TransportChecker{
  6189  		checker.SrcPort(context.StackPort),
  6190  		checker.DstPort(srcPort),
  6191  		checker.TCPFlags(header.TCPFlagAck | header.TCPFlagSyn),
  6192  		checker.TCPAckNum(uint32(irs) + 1),
  6193  	}
  6194  
  6195  	if synCookieInUse {
  6196  		// When cookies are in use window scaling is disabled.
  6197  		tcpCheckers = append(tcpCheckers, checker.TCPSynOptions(header.TCPSynOptions{
  6198  			WS:  -1,
  6199  			MSS: c.MSSWithoutOptionsV6(),
  6200  		}))
  6201  	} else {
  6202  		tcpCheckers = append(tcpCheckers, checker.TCPSynOptions(header.TCPSynOptions{
  6203  			WS:  tcp.FindWndScale(tcp.DefaultReceiveBufferSize),
  6204  			MSS: c.MSSWithoutOptionsV6(),
  6205  		}))
  6206  	}
  6207  
  6208  	checker.IPv6(t, b, checker.TCP(tcpCheckers...))
  6209  
  6210  	// Send ACK.
  6211  	c.SendV6Packet(nil, &context.Headers{
  6212  		SrcPort: srcPort,
  6213  		DstPort: context.StackPort,
  6214  		Flags:   header.TCPFlagAck,
  6215  		SeqNum:  irs + 1,
  6216  		AckNum:  iss + 1,
  6217  		RcvWnd:  30000,
  6218  	})
  6219  	return irs, iss
  6220  }
  6221  
  6222  // TestListenBacklogFull tests that netstack does not complete handshakes if the
  6223  // listen backlog for the endpoint is full.
  6224  func TestListenBacklogFull(t *testing.T) {
  6225  	c := context.New(t, e2e.DefaultMTU)
  6226  	defer c.Cleanup()
  6227  
  6228  	// Create TCP endpoint.
  6229  	var err tcpip.Error
  6230  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  6231  	if err != nil {
  6232  		t.Fatalf("NewEndpoint failed: %s", err)
  6233  	}
  6234  
  6235  	// Bind to wildcard.
  6236  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  6237  		t.Fatalf("Bind failed: %s", err)
  6238  	}
  6239  
  6240  	// Test acceptance.
  6241  	// Start listening.
  6242  	listenBacklog := 10
  6243  	if err := c.EP.Listen(listenBacklog); err != nil {
  6244  		t.Fatalf("Listen failed: %s", err)
  6245  	}
  6246  
  6247  	lastPortOffset := uint16(0)
  6248  	for ; int(lastPortOffset) < listenBacklog; lastPortOffset++ {
  6249  		executeHandshake(t, c, context.TestPort+lastPortOffset, false /*synCookieInUse */)
  6250  	}
  6251  
  6252  	time.Sleep(50 * time.Millisecond)
  6253  
  6254  	// Now execute send one more SYN. The stack should not respond as the backlog
  6255  	// is full at this point.
  6256  	c.SendPacket(nil, &context.Headers{
  6257  		SrcPort: context.TestPort + lastPortOffset,
  6258  		DstPort: context.StackPort,
  6259  		Flags:   header.TCPFlagSyn,
  6260  		SeqNum:  seqnum.Value(context.TestInitialSequenceNumber),
  6261  		RcvWnd:  30000,
  6262  	})
  6263  	c.CheckNoPacketTimeout("unexpected packet received", 50*time.Millisecond)
  6264  
  6265  	// Try to accept the connections in the backlog.
  6266  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  6267  	c.WQ.EventRegister(&we)
  6268  	defer c.WQ.EventUnregister(&we)
  6269  
  6270  	for i := 0; i < listenBacklog; i++ {
  6271  		_, _, err = c.EP.Accept(nil)
  6272  		if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  6273  			// Wait for connection to be established.
  6274  			select {
  6275  			case <-ch:
  6276  				_, _, err = c.EP.Accept(nil)
  6277  				if err != nil {
  6278  					t.Fatalf("Accept failed: %s", err)
  6279  				}
  6280  
  6281  			case <-time.After(1 * time.Second):
  6282  				t.Fatalf("Timed out waiting for accept")
  6283  			}
  6284  		}
  6285  	}
  6286  
  6287  	// Now verify that there are no more connections that can be accepted.
  6288  	_, _, err = c.EP.Accept(nil)
  6289  	if !cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  6290  		select {
  6291  		case <-ch:
  6292  			t.Fatalf("unexpected endpoint delivered on Accept: %+v", c.EP)
  6293  		case <-time.After(1 * time.Second):
  6294  		}
  6295  	}
  6296  
  6297  	// Now a new handshake must succeed.
  6298  	executeHandshake(t, c, context.TestPort+lastPortOffset, false /*synCookieInUse */)
  6299  
  6300  	newEP, _, err := c.EP.Accept(nil)
  6301  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  6302  		// Wait for connection to be established.
  6303  		select {
  6304  		case <-ch:
  6305  			newEP, _, err = c.EP.Accept(nil)
  6306  			if err != nil {
  6307  				t.Fatalf("Accept failed: %s", err)
  6308  			}
  6309  
  6310  		case <-time.After(1 * time.Second):
  6311  			t.Fatalf("Timed out waiting for accept")
  6312  		}
  6313  	}
  6314  
  6315  	// Now verify that the TCP socket is usable and in a connected state.
  6316  	data := "Don't panic"
  6317  	var r strings.Reader
  6318  	r.Reset(data)
  6319  	newEP.Write(&r, tcpip.WriteOptions{})
  6320  	b := c.GetPacket()
  6321  	defer b.Release()
  6322  	tcp := header.TCP(header.IPv4(b.AsSlice()).Payload())
  6323  	if string(tcp.Payload()) != data {
  6324  		t.Fatalf("unexpected data: got %s, want %s", string(tcp.Payload()), data)
  6325  	}
  6326  }
  6327  
  6328  // TestListenNoAcceptMulticastBroadcastV4 makes sure that TCP segments with a
  6329  // non unicast IPv4 address are not accepted.
  6330  func TestListenNoAcceptNonUnicastV4(t *testing.T) {
  6331  	multicastAddr := tcpiptestutil.MustParse4("224.0.1.2")
  6332  	otherMulticastAddr := tcpiptestutil.MustParse4("224.0.1.3")
  6333  	subnet := context.StackAddrWithPrefix.Subnet()
  6334  	subnetBroadcastAddr := subnet.Broadcast()
  6335  
  6336  	tests := []struct {
  6337  		name    string
  6338  		srcAddr tcpip.Address
  6339  		dstAddr tcpip.Address
  6340  	}{
  6341  		{
  6342  			name:    "SourceUnspecified",
  6343  			srcAddr: header.IPv4Any,
  6344  			dstAddr: context.StackAddr,
  6345  		},
  6346  		{
  6347  			name:    "SourceBroadcast",
  6348  			srcAddr: header.IPv4Broadcast,
  6349  			dstAddr: context.StackAddr,
  6350  		},
  6351  		{
  6352  			name:    "SourceOurMulticast",
  6353  			srcAddr: multicastAddr,
  6354  			dstAddr: context.StackAddr,
  6355  		},
  6356  		{
  6357  			name:    "SourceOtherMulticast",
  6358  			srcAddr: otherMulticastAddr,
  6359  			dstAddr: context.StackAddr,
  6360  		},
  6361  		{
  6362  			name:    "DestUnspecified",
  6363  			srcAddr: context.TestAddr,
  6364  			dstAddr: header.IPv4Any,
  6365  		},
  6366  		{
  6367  			name:    "DestBroadcast",
  6368  			srcAddr: context.TestAddr,
  6369  			dstAddr: header.IPv4Broadcast,
  6370  		},
  6371  		{
  6372  			name:    "DestOurMulticast",
  6373  			srcAddr: context.TestAddr,
  6374  			dstAddr: multicastAddr,
  6375  		},
  6376  		{
  6377  			name:    "DestOtherMulticast",
  6378  			srcAddr: context.TestAddr,
  6379  			dstAddr: otherMulticastAddr,
  6380  		},
  6381  		{
  6382  			name:    "SrcSubnetBroadcast",
  6383  			srcAddr: subnetBroadcastAddr,
  6384  			dstAddr: context.StackAddr,
  6385  		},
  6386  		{
  6387  			name:    "DestSubnetBroadcast",
  6388  			srcAddr: context.TestAddr,
  6389  			dstAddr: subnetBroadcastAddr,
  6390  		},
  6391  	}
  6392  
  6393  	for _, test := range tests {
  6394  		t.Run(test.name, func(t *testing.T) {
  6395  			c := context.New(t, e2e.DefaultMTU)
  6396  			defer c.Cleanup()
  6397  
  6398  			c.Create(-1)
  6399  
  6400  			if err := c.Stack().JoinGroup(header.IPv4ProtocolNumber, 1, multicastAddr); err != nil {
  6401  				t.Fatalf("JoinGroup failed: %s", err)
  6402  			}
  6403  
  6404  			if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  6405  				t.Fatalf("Bind failed: %s", err)
  6406  			}
  6407  
  6408  			if err := c.EP.Listen(1); err != nil {
  6409  				t.Fatalf("Listen failed: %s", err)
  6410  			}
  6411  
  6412  			irs := seqnum.Value(context.TestInitialSequenceNumber)
  6413  			c.SendPacketWithAddrs(nil, &context.Headers{
  6414  				SrcPort: context.TestPort,
  6415  				DstPort: context.StackPort,
  6416  				Flags:   header.TCPFlagSyn,
  6417  				SeqNum:  irs,
  6418  				RcvWnd:  30000,
  6419  			}, test.srcAddr, test.dstAddr)
  6420  			c.CheckNoPacket("Should not have received a response")
  6421  
  6422  			// Handle normal packet.
  6423  			c.SendPacketWithAddrs(nil, &context.Headers{
  6424  				SrcPort: context.TestPort,
  6425  				DstPort: context.StackPort,
  6426  				Flags:   header.TCPFlagSyn,
  6427  				SeqNum:  irs,
  6428  				RcvWnd:  30000,
  6429  			}, context.TestAddr, context.StackAddr)
  6430  			v := c.GetPacket()
  6431  			defer v.Release()
  6432  			checker.IPv4(t, v,
  6433  				checker.TCP(
  6434  					checker.SrcPort(context.StackPort),
  6435  					checker.DstPort(context.TestPort),
  6436  					checker.TCPFlags(header.TCPFlagAck|header.TCPFlagSyn),
  6437  					checker.TCPAckNum(uint32(irs)+1)))
  6438  		})
  6439  	}
  6440  }
  6441  
  6442  // TestListenNoAcceptMulticastBroadcastV6 makes sure that TCP segments with a
  6443  // non unicast IPv6 address are not accepted.
  6444  func TestListenNoAcceptNonUnicastV6(t *testing.T) {
  6445  	multicastAddr := tcpiptestutil.MustParse6("ff0e::101")
  6446  	otherMulticastAddr := tcpiptestutil.MustParse6("ff0e::102")
  6447  
  6448  	tests := []struct {
  6449  		name    string
  6450  		srcAddr tcpip.Address
  6451  		dstAddr tcpip.Address
  6452  	}{
  6453  		{
  6454  			"SourceUnspecified",
  6455  			header.IPv6Any,
  6456  			context.StackV6Addr,
  6457  		},
  6458  		{
  6459  			"SourceAllNodes",
  6460  			header.IPv6AllNodesMulticastAddress,
  6461  			context.StackV6Addr,
  6462  		},
  6463  		{
  6464  			"SourceOurMulticast",
  6465  			multicastAddr,
  6466  			context.StackV6Addr,
  6467  		},
  6468  		{
  6469  			"SourceOtherMulticast",
  6470  			otherMulticastAddr,
  6471  			context.StackV6Addr,
  6472  		},
  6473  		{
  6474  			"DestUnspecified",
  6475  			context.TestV6Addr,
  6476  			header.IPv6Any,
  6477  		},
  6478  		{
  6479  			"DestAllNodes",
  6480  			context.TestV6Addr,
  6481  			header.IPv6AllNodesMulticastAddress,
  6482  		},
  6483  		{
  6484  			"DestOurMulticast",
  6485  			context.TestV6Addr,
  6486  			multicastAddr,
  6487  		},
  6488  		{
  6489  			"DestOtherMulticast",
  6490  			context.TestV6Addr,
  6491  			otherMulticastAddr,
  6492  		},
  6493  	}
  6494  
  6495  	for _, test := range tests {
  6496  		t.Run(test.name, func(t *testing.T) {
  6497  			c := context.New(t, e2e.DefaultMTU)
  6498  			defer c.Cleanup()
  6499  
  6500  			c.CreateV6Endpoint(true)
  6501  
  6502  			if err := c.Stack().JoinGroup(header.IPv6ProtocolNumber, 1, multicastAddr); err != nil {
  6503  				t.Fatalf("JoinGroup failed: %s", err)
  6504  			}
  6505  
  6506  			if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  6507  				t.Fatalf("Bind failed: %s", err)
  6508  			}
  6509  
  6510  			if err := c.EP.Listen(1); err != nil {
  6511  				t.Fatalf("Listen failed: %s", err)
  6512  			}
  6513  
  6514  			irs := seqnum.Value(context.TestInitialSequenceNumber)
  6515  			c.SendV6PacketWithAddrs(nil, &context.Headers{
  6516  				SrcPort: context.TestPort,
  6517  				DstPort: context.StackPort,
  6518  				Flags:   header.TCPFlagSyn,
  6519  				SeqNum:  irs,
  6520  				RcvWnd:  30000,
  6521  			}, test.srcAddr, test.dstAddr)
  6522  			c.CheckNoPacket("Should not have received a response")
  6523  
  6524  			// Handle normal packet.
  6525  			c.SendV6PacketWithAddrs(nil, &context.Headers{
  6526  				SrcPort: context.TestPort,
  6527  				DstPort: context.StackPort,
  6528  				Flags:   header.TCPFlagSyn,
  6529  				SeqNum:  irs,
  6530  				RcvWnd:  30000,
  6531  			}, context.TestV6Addr, context.StackV6Addr)
  6532  			v := c.GetV6Packet()
  6533  			defer v.Release()
  6534  			checker.IPv6(t, v,
  6535  				checker.TCP(
  6536  					checker.SrcPort(context.StackPort),
  6537  					checker.DstPort(context.TestPort),
  6538  					checker.TCPFlags(header.TCPFlagAck|header.TCPFlagSyn),
  6539  					checker.TCPAckNum(uint32(irs)+1)))
  6540  		})
  6541  	}
  6542  }
  6543  
  6544  func TestListenSynRcvdQueueFull(t *testing.T) {
  6545  	c := context.New(t, e2e.DefaultMTU)
  6546  	defer c.Cleanup()
  6547  
  6548  	// Create TCP endpoint.
  6549  	var err tcpip.Error
  6550  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  6551  	if err != nil {
  6552  		t.Fatalf("NewEndpoint failed: %s", err)
  6553  	}
  6554  
  6555  	// Bind to wildcard.
  6556  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  6557  		t.Fatalf("Bind failed: %s", err)
  6558  	}
  6559  
  6560  	// Test acceptance.
  6561  	if err := c.EP.Listen(1); err != nil {
  6562  		t.Fatalf("Listen failed: %s", err)
  6563  	}
  6564  
  6565  	// Send two SYN's the first one should get a SYN-ACK, the
  6566  	// second one should not get any response and is dropped as
  6567  	// the accept queue is full.
  6568  	irs := seqnum.Value(context.TestInitialSequenceNumber)
  6569  	c.SendPacket(nil, &context.Headers{
  6570  		SrcPort: context.TestPort,
  6571  		DstPort: context.StackPort,
  6572  		Flags:   header.TCPFlagSyn,
  6573  		SeqNum:  irs,
  6574  		RcvWnd:  30000,
  6575  	})
  6576  
  6577  	// Receive the SYN-ACK reply.
  6578  	b := c.GetPacket()
  6579  	defer b.Release()
  6580  	tcp := header.TCP(header.IPv4(b.AsSlice()).Payload())
  6581  	iss := seqnum.Value(tcp.SequenceNumber())
  6582  	tcpCheckers := []checker.TransportChecker{
  6583  		checker.SrcPort(context.StackPort),
  6584  		checker.DstPort(context.TestPort),
  6585  		checker.TCPFlags(header.TCPFlagAck | header.TCPFlagSyn),
  6586  		checker.TCPAckNum(uint32(irs) + 1),
  6587  	}
  6588  	checker.IPv4(t, b, checker.TCP(tcpCheckers...))
  6589  
  6590  	// Now complete the previous connection.
  6591  	// Send ACK.
  6592  	c.SendPacket(nil, &context.Headers{
  6593  		SrcPort: context.TestPort,
  6594  		DstPort: context.StackPort,
  6595  		Flags:   header.TCPFlagAck,
  6596  		SeqNum:  irs + 1,
  6597  		AckNum:  iss + 1,
  6598  		RcvWnd:  30000,
  6599  	})
  6600  
  6601  	// Verify if that is delivered to the accept queue.
  6602  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  6603  	c.WQ.EventRegister(&we)
  6604  	defer c.WQ.EventUnregister(&we)
  6605  	<-ch
  6606  
  6607  	// Now execute send one more SYN. The stack should not respond as the backlog
  6608  	// is full at this point.
  6609  	c.SendPacket(nil, &context.Headers{
  6610  		SrcPort: context.TestPort + 1,
  6611  		DstPort: context.StackPort,
  6612  		Flags:   header.TCPFlagSyn,
  6613  		SeqNum:  seqnum.Value(889),
  6614  		RcvWnd:  30000,
  6615  	})
  6616  	c.CheckNoPacketTimeout("unexpected packet received", 50*time.Millisecond)
  6617  
  6618  	// Try to accept the connections in the backlog.
  6619  	newEP, _, err := c.EP.Accept(nil)
  6620  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  6621  		// Wait for connection to be established.
  6622  		select {
  6623  		case <-ch:
  6624  			newEP, _, err = c.EP.Accept(nil)
  6625  			if err != nil {
  6626  				t.Fatalf("Accept failed: %s", err)
  6627  			}
  6628  
  6629  		case <-time.After(1 * time.Second):
  6630  			t.Fatalf("Timed out waiting for accept")
  6631  		}
  6632  	}
  6633  
  6634  	// Now verify that the TCP socket is usable and in a connected state.
  6635  	data := "Don't panic"
  6636  	var r strings.Reader
  6637  	r.Reset(data)
  6638  	newEP.Write(&r, tcpip.WriteOptions{})
  6639  	pkt := c.GetPacket()
  6640  	defer pkt.Release()
  6641  	tcp = header.IPv4(pkt.AsSlice()).Payload()
  6642  	if string(tcp.Payload()) != data {
  6643  		t.Fatalf("unexpected data: got %s, want %s", string(tcp.Payload()), data)
  6644  	}
  6645  }
  6646  
  6647  func TestListenBacklogFullSynCookieInUse(t *testing.T) {
  6648  	c := context.New(t, e2e.DefaultMTU)
  6649  	defer c.Cleanup()
  6650  
  6651  	// Create TCP endpoint.
  6652  	var err tcpip.Error
  6653  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  6654  	if err != nil {
  6655  		t.Fatalf("NewEndpoint failed: %s", err)
  6656  	}
  6657  
  6658  	// Bind to wildcard.
  6659  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  6660  		t.Fatalf("Bind failed: %s", err)
  6661  	}
  6662  
  6663  	// Test for SynCookies usage after filling up the backlog.
  6664  	if err := c.EP.Listen(1); err != nil {
  6665  		t.Fatalf("Listen failed: %s", err)
  6666  	}
  6667  
  6668  	executeHandshake(t, c, context.TestPort, true)
  6669  
  6670  	// Wait for this to be delivered to the accept queue.
  6671  	time.Sleep(50 * time.Millisecond)
  6672  
  6673  	// Send a SYN request.
  6674  	irs := seqnum.Value(context.TestInitialSequenceNumber)
  6675  	c.SendPacket(nil, &context.Headers{
  6676  		// pick a different src port for new SYN.
  6677  		SrcPort: context.TestPort + 1,
  6678  		DstPort: context.StackPort,
  6679  		Flags:   header.TCPFlagSyn,
  6680  		SeqNum:  irs,
  6681  		RcvWnd:  30000,
  6682  	})
  6683  	// The Syn should be dropped as the endpoint's backlog is full.
  6684  	c.CheckNoPacketTimeout("unexpected packet received", 50*time.Millisecond)
  6685  
  6686  	// Verify that there is only one acceptable connection at this point.
  6687  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  6688  	c.WQ.EventRegister(&we)
  6689  	defer c.WQ.EventUnregister(&we)
  6690  
  6691  	_, _, err = c.EP.Accept(nil)
  6692  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  6693  		// Wait for connection to be established.
  6694  		select {
  6695  		case <-ch:
  6696  			_, _, err = c.EP.Accept(nil)
  6697  			if err != nil {
  6698  				t.Fatalf("Accept failed: %s", err)
  6699  			}
  6700  
  6701  		case <-time.After(1 * time.Second):
  6702  			t.Fatalf("Timed out waiting for accept")
  6703  		}
  6704  	}
  6705  
  6706  	// Now verify that there are no more connections that can be accepted.
  6707  	_, _, err = c.EP.Accept(nil)
  6708  	if !cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  6709  		select {
  6710  		case <-ch:
  6711  			t.Fatalf("unexpected endpoint delivered on Accept: %+v", c.EP)
  6712  		case <-time.After(1 * time.Second):
  6713  		}
  6714  	}
  6715  }
  6716  
  6717  func TestSYNRetransmit(t *testing.T) {
  6718  	c := context.New(t, e2e.DefaultMTU)
  6719  	defer c.Cleanup()
  6720  
  6721  	// Create TCP endpoint.
  6722  	var err tcpip.Error
  6723  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  6724  	if err != nil {
  6725  		t.Fatalf("NewEndpoint failed: %s", err)
  6726  	}
  6727  
  6728  	// Bind to wildcard.
  6729  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  6730  		t.Fatalf("Bind failed: %s", err)
  6731  	}
  6732  
  6733  	// Start listening.
  6734  	if err := c.EP.Listen(10); err != nil {
  6735  		t.Fatalf("Listen failed: %s", err)
  6736  	}
  6737  
  6738  	// Send the same SYN packet multiple times. We should still get a valid SYN-ACK
  6739  	// reply.
  6740  	irs := seqnum.Value(context.TestInitialSequenceNumber)
  6741  	for i := 0; i < 5; i++ {
  6742  		c.SendPacket(nil, &context.Headers{
  6743  			SrcPort: context.TestPort,
  6744  			DstPort: context.StackPort,
  6745  			Flags:   header.TCPFlagSyn,
  6746  			SeqNum:  irs,
  6747  			RcvWnd:  30000,
  6748  		})
  6749  	}
  6750  
  6751  	// Receive the SYN-ACK reply.
  6752  	tcpCheckers := []checker.TransportChecker{
  6753  		checker.SrcPort(context.StackPort),
  6754  		checker.DstPort(context.TestPort),
  6755  		checker.TCPFlags(header.TCPFlagAck | header.TCPFlagSyn),
  6756  		checker.TCPAckNum(uint32(irs) + 1),
  6757  	}
  6758  	v := c.GetPacket()
  6759  	defer v.Release()
  6760  	checker.IPv4(t, v, checker.TCP(tcpCheckers...))
  6761  }
  6762  
  6763  func TestSynRcvdBadSeqNumber(t *testing.T) {
  6764  	c := context.New(t, e2e.DefaultMTU)
  6765  	defer c.Cleanup()
  6766  
  6767  	// Create TCP endpoint.
  6768  	var err tcpip.Error
  6769  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  6770  	if err != nil {
  6771  		t.Fatalf("NewEndpoint failed: %s", err)
  6772  	}
  6773  
  6774  	// Bind to wildcard.
  6775  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  6776  		t.Fatalf("Bind failed: %s", err)
  6777  	}
  6778  
  6779  	// Start listening.
  6780  	if err := c.EP.Listen(10); err != nil {
  6781  		t.Fatalf("Listen failed: %s", err)
  6782  	}
  6783  
  6784  	// Send a SYN to get a SYN-ACK. This should put the ep into SYN-RCVD state
  6785  	irs := seqnum.Value(context.TestInitialSequenceNumber)
  6786  	c.SendPacket(nil, &context.Headers{
  6787  		SrcPort: context.TestPort,
  6788  		DstPort: context.StackPort,
  6789  		Flags:   header.TCPFlagSyn,
  6790  		SeqNum:  irs,
  6791  		RcvWnd:  30000,
  6792  	})
  6793  
  6794  	// Receive the SYN-ACK reply.
  6795  	b := c.GetPacket()
  6796  	defer b.Release()
  6797  	tcpHdr := header.TCP(header.IPv4(b.AsSlice()).Payload())
  6798  	iss := seqnum.Value(tcpHdr.SequenceNumber())
  6799  	tcpCheckers := []checker.TransportChecker{
  6800  		checker.SrcPort(context.StackPort),
  6801  		checker.DstPort(context.TestPort),
  6802  		checker.TCPFlags(header.TCPFlagAck | header.TCPFlagSyn),
  6803  		checker.TCPAckNum(uint32(irs) + 1),
  6804  	}
  6805  	checker.IPv4(t, b, checker.TCP(tcpCheckers...))
  6806  
  6807  	// Now send a packet with an out-of-window sequence number
  6808  	largeSeqnum := irs + seqnum.Value(tcpHdr.WindowSize()) + 1
  6809  	c.SendPacket(nil, &context.Headers{
  6810  		SrcPort: context.TestPort,
  6811  		DstPort: context.StackPort,
  6812  		Flags:   header.TCPFlagAck,
  6813  		SeqNum:  largeSeqnum,
  6814  		AckNum:  iss + 1,
  6815  		RcvWnd:  30000,
  6816  	})
  6817  
  6818  	// Should receive an ACK with the expected SEQ number
  6819  	b = c.GetPacket()
  6820  	defer b.Release()
  6821  	tcpCheckers = []checker.TransportChecker{
  6822  		checker.SrcPort(context.StackPort),
  6823  		checker.DstPort(context.TestPort),
  6824  		checker.TCPFlags(header.TCPFlagAck),
  6825  		checker.TCPAckNum(uint32(irs) + 1),
  6826  		checker.TCPSeqNum(uint32(iss + 1)),
  6827  	}
  6828  	checker.IPv4(t, b, checker.TCP(tcpCheckers...))
  6829  
  6830  	// Now that the socket replied appropriately with the ACK,
  6831  	// complete the connection to test that the large SEQ num
  6832  	// did not change the state from SYN-RCVD.
  6833  
  6834  	// Get setup to be notified about connection establishment.
  6835  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  6836  	c.WQ.EventRegister(&we)
  6837  	defer c.WQ.EventUnregister(&we)
  6838  
  6839  	// Send ACK to move to ESTABLISHED state.
  6840  	c.SendPacket(nil, &context.Headers{
  6841  		SrcPort: context.TestPort,
  6842  		DstPort: context.StackPort,
  6843  		Flags:   header.TCPFlagAck,
  6844  		SeqNum:  irs + 1,
  6845  		AckNum:  iss + 1,
  6846  		RcvWnd:  30000,
  6847  	})
  6848  
  6849  	<-ch
  6850  	newEP, _, err := c.EP.Accept(nil)
  6851  	if err != nil {
  6852  		t.Fatalf("Accept failed: %s", err)
  6853  	}
  6854  
  6855  	// Now verify that the TCP socket is usable and in a connected state.
  6856  	data := "Don't panic"
  6857  	var r strings.Reader
  6858  	r.Reset(data)
  6859  	if _, err := newEP.Write(&r, tcpip.WriteOptions{}); err != nil {
  6860  		t.Fatalf("Write failed: %s", err)
  6861  	}
  6862  
  6863  	pkt := c.GetPacket()
  6864  	defer pkt.Release()
  6865  	tcpHdr = header.IPv4(pkt.AsSlice()).Payload()
  6866  	if string(tcpHdr.Payload()) != data {
  6867  		t.Fatalf("unexpected data: got %s, want %s", string(tcpHdr.Payload()), data)
  6868  	}
  6869  }
  6870  
  6871  func TestPassiveConnectionAttemptIncrement(t *testing.T) {
  6872  	c := context.New(t, e2e.DefaultMTU)
  6873  	defer c.Cleanup()
  6874  
  6875  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  6876  	if err != nil {
  6877  		t.Fatalf("NewEndpoint failed: %s", err)
  6878  	}
  6879  	c.EP = ep
  6880  	if err := ep.Bind(tcpip.FullAddress{Addr: context.StackAddr, Port: context.StackPort}); err != nil {
  6881  		t.Fatalf("Bind failed: %s", err)
  6882  	}
  6883  	if got, want := tcp.EndpointState(ep.State()), tcp.StateBound; got != want {
  6884  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
  6885  	}
  6886  	if err := c.EP.Listen(1); err != nil {
  6887  		t.Fatalf("Listen failed: %s", err)
  6888  	}
  6889  	if got, want := tcp.EndpointState(c.EP.State()), tcp.StateListen; got != want {
  6890  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
  6891  	}
  6892  
  6893  	stats := c.Stack().Stats()
  6894  	want := stats.TCP.PassiveConnectionOpenings.Value() + 1
  6895  
  6896  	srcPort := uint16(context.TestPort)
  6897  	executeHandshake(t, c, srcPort+1, true /* synCookiesInUse */)
  6898  
  6899  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  6900  	c.WQ.EventRegister(&we)
  6901  	defer c.WQ.EventUnregister(&we)
  6902  
  6903  	// Verify that there is only one acceptable connection at this point.
  6904  	_, _, err = c.EP.Accept(nil)
  6905  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  6906  		// Wait for connection to be established.
  6907  		select {
  6908  		case <-ch:
  6909  			_, _, err = c.EP.Accept(nil)
  6910  			if err != nil {
  6911  				t.Fatalf("Accept failed: %s", err)
  6912  			}
  6913  
  6914  		case <-time.After(1 * time.Second):
  6915  			t.Fatalf("Timed out waiting for accept")
  6916  		}
  6917  	}
  6918  
  6919  	if got := stats.TCP.PassiveConnectionOpenings.Value(); got != want {
  6920  		t.Errorf("got stats.TCP.PassiveConnectionOpenings.Value() = %d, want = %d", got, want)
  6921  	}
  6922  }
  6923  
  6924  func TestPassiveFailedConnectionAttemptIncrement(t *testing.T) {
  6925  	c := context.New(t, e2e.DefaultMTU)
  6926  	defer c.Cleanup()
  6927  
  6928  	stats := c.Stack().Stats()
  6929  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  6930  	if err != nil {
  6931  		t.Fatalf("NewEndpoint failed: %s", err)
  6932  	}
  6933  	c.EP = ep
  6934  	if err := c.EP.Bind(tcpip.FullAddress{Addr: context.StackAddr, Port: context.StackPort}); err != nil {
  6935  		t.Fatalf("Bind failed: %s", err)
  6936  	}
  6937  	if err := c.EP.Listen(1); err != nil {
  6938  		t.Fatalf("Listen failed: %s", err)
  6939  	}
  6940  
  6941  	srcPort := uint16(context.TestPort)
  6942  	// Now attempt a handshakes it will fill up the accept backlog.
  6943  	executeHandshake(t, c, srcPort, true /* synCookesInUse */)
  6944  
  6945  	// Give time for the final ACK to be processed as otherwise the next handshake could
  6946  	// get accepted before the previous one based on goroutine scheduling.
  6947  	time.Sleep(50 * time.Millisecond)
  6948  
  6949  	want := stats.TCP.ListenOverflowSynDrop.Value() + 1
  6950  
  6951  	// Now we will send one more SYN and this one should get dropped
  6952  	// Send a SYN request.
  6953  	c.SendPacket(nil, &context.Headers{
  6954  		SrcPort: srcPort + 2,
  6955  		DstPort: context.StackPort,
  6956  		Flags:   header.TCPFlagSyn,
  6957  		SeqNum:  seqnum.Value(context.TestInitialSequenceNumber),
  6958  		RcvWnd:  30000,
  6959  	})
  6960  
  6961  	checkValid := func() []error {
  6962  		var errors []error
  6963  		if got := stats.TCP.ListenOverflowSynDrop.Value(); got != want {
  6964  			errors = append(errors, fmt.Errorf("got stats.TCP.ListenOverflowSynDrop.Value() = %d, want = %d", got, want))
  6965  		}
  6966  		if got := c.EP.Stats().(*tcp.Stats).ReceiveErrors.ListenOverflowSynDrop.Value(); got != want {
  6967  			errors = append(errors, fmt.Errorf("got EP stats Stats.ReceiveErrors.ListenOverflowSynDrop = %d, want = %d", got, want))
  6968  		}
  6969  		return errors
  6970  	}
  6971  
  6972  	start := time.Now()
  6973  	for time.Since(start) < time.Minute && len(checkValid()) > 0 {
  6974  		time.Sleep(50 * time.Millisecond)
  6975  	}
  6976  	for _, err := range checkValid() {
  6977  		t.Error(err)
  6978  	}
  6979  	if t.Failed() {
  6980  		t.FailNow()
  6981  	}
  6982  
  6983  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  6984  	c.WQ.EventRegister(&we)
  6985  	defer c.WQ.EventUnregister(&we)
  6986  
  6987  	// Now check that there is one acceptable connections.
  6988  	_, _, err = c.EP.Accept(nil)
  6989  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  6990  		// Wait for connection to be established.
  6991  		<-ch
  6992  		_, _, err = c.EP.Accept(nil)
  6993  		if err != nil {
  6994  			t.Fatalf("Accept failed: %s", err)
  6995  		}
  6996  	}
  6997  }
  6998  
  6999  func TestListenDropIncrement(t *testing.T) {
  7000  	c := context.New(t, e2e.DefaultMTU)
  7001  	defer c.Cleanup()
  7002  
  7003  	stats := c.Stack().Stats()
  7004  	c.Create(-1 /*epRcvBuf*/)
  7005  
  7006  	if err := c.EP.Bind(tcpip.FullAddress{Addr: context.StackAddr, Port: context.StackPort}); err != nil {
  7007  		t.Fatalf("Bind failed: %s", err)
  7008  	}
  7009  	if err := c.EP.Listen(1 /*backlog*/); err != nil {
  7010  		t.Fatalf("Listen failed: %s", err)
  7011  	}
  7012  
  7013  	initialDropped := stats.DroppedPackets.Value()
  7014  
  7015  	// Send RST, FIN segments, that are expected to be dropped by the listener.
  7016  	c.SendPacket(nil, &context.Headers{
  7017  		SrcPort: context.TestPort,
  7018  		DstPort: context.StackPort,
  7019  		Flags:   header.TCPFlagRst,
  7020  	})
  7021  	c.SendPacket(nil, &context.Headers{
  7022  		SrcPort: context.TestPort,
  7023  		DstPort: context.StackPort,
  7024  		Flags:   header.TCPFlagFin,
  7025  	})
  7026  
  7027  	// To ensure that the RST, FIN sent earlier are indeed received and ignored
  7028  	// by the listener, send a SYN and wait for the SYN to be ACKd.
  7029  	irs := seqnum.Value(context.TestInitialSequenceNumber)
  7030  	c.SendPacket(nil, &context.Headers{
  7031  		SrcPort: context.TestPort,
  7032  		DstPort: context.StackPort,
  7033  		Flags:   header.TCPFlagSyn,
  7034  		SeqNum:  irs,
  7035  	})
  7036  	v := c.GetPacket()
  7037  	defer v.Release()
  7038  	checker.IPv4(t, v, checker.TCP(checker.SrcPort(context.StackPort),
  7039  		checker.DstPort(context.TestPort),
  7040  		checker.TCPFlags(header.TCPFlagAck|header.TCPFlagSyn),
  7041  		checker.TCPAckNum(uint32(irs)+1),
  7042  	))
  7043  
  7044  	if got, want := stats.DroppedPackets.Value(), initialDropped+2; got != want {
  7045  		t.Fatalf("got stats.DroppedPackets.Value() = %d, want = %d", got, want)
  7046  	}
  7047  }
  7048  
  7049  func TestEndpointBindListenAcceptState(t *testing.T) {
  7050  	c := context.New(t, e2e.DefaultMTU)
  7051  	defer c.Cleanup()
  7052  	wq := &waiter.Queue{}
  7053  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  7054  	if err != nil {
  7055  		t.Fatalf("NewEndpoint failed: %s", err)
  7056  	}
  7057  
  7058  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  7059  		t.Fatalf("Bind failed: %s", err)
  7060  	}
  7061  	if got, want := tcp.EndpointState(ep.State()), tcp.StateBound; got != want {
  7062  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
  7063  	}
  7064  
  7065  	ept := endpointTester{ep}
  7066  	ept.CheckReadError(t, &tcpip.ErrNotConnected{})
  7067  	if got := ep.Stats().(*tcp.Stats).ReadErrors.NotConnected.Value(); got != 1 {
  7068  		t.Errorf("got EP stats Stats.ReadErrors.NotConnected got %d want %d", got, 1)
  7069  	}
  7070  
  7071  	if err := ep.Listen(10); err != nil {
  7072  		t.Fatalf("Listen failed: %s", err)
  7073  	}
  7074  	if got, want := tcp.EndpointState(ep.State()), tcp.StateListen; got != want {
  7075  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
  7076  	}
  7077  
  7078  	c.PassiveConnectWithOptions(100, 5, header.TCPSynOptions{MSS: e2e.DefaultIPv4MSS}, 0 /* delay */)
  7079  
  7080  	// Try to accept the connection.
  7081  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  7082  	wq.EventRegister(&we)
  7083  	defer wq.EventUnregister(&we)
  7084  
  7085  	aep, _, err := ep.Accept(nil)
  7086  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  7087  		// Wait for connection to be established.
  7088  		select {
  7089  		case <-ch:
  7090  			aep, _, err = ep.Accept(nil)
  7091  			if err != nil {
  7092  				t.Fatalf("Accept failed: %s", err)
  7093  			}
  7094  
  7095  		case <-time.After(1 * time.Second):
  7096  			t.Fatalf("Timed out waiting for accept")
  7097  		}
  7098  	}
  7099  	if got, want := tcp.EndpointState(aep.State()), tcp.StateEstablished; got != want {
  7100  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
  7101  	}
  7102  	{
  7103  		err := aep.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort})
  7104  		if d := cmp.Diff(&tcpip.ErrAlreadyConnected{}, err); d != "" {
  7105  			t.Errorf("Connect(...) mismatch (-want +got):\n%s", d)
  7106  		}
  7107  	}
  7108  	// Listening endpoint remains in listen state.
  7109  	if got, want := tcp.EndpointState(ep.State()), tcp.StateListen; got != want {
  7110  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
  7111  	}
  7112  
  7113  	ep.Close()
  7114  	// Give worker goroutines time to receive the close notification.
  7115  	time.Sleep(1 * time.Second)
  7116  	if got, want := tcp.EndpointState(ep.State()), tcp.StateClose; got != want {
  7117  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
  7118  	}
  7119  	// Accepted endpoint remains open when the listen endpoint is closed.
  7120  	if got, want := tcp.EndpointState(aep.State()), tcp.StateEstablished; got != want {
  7121  		t.Errorf("unexpected endpoint state: want %s, got %s", want, got)
  7122  	}
  7123  
  7124  }
  7125  
  7126  // This test verifies that the auto tuning does not grow the receive buffer if
  7127  // the application is not reading the data actively.
  7128  func TestReceiveBufferAutoTuningApplicationLimited(t *testing.T) {
  7129  	const mtu = 1500
  7130  	const mss = mtu - header.IPv4MinimumSize - header.TCPMinimumSize
  7131  
  7132  	c := context.New(t, mtu)
  7133  	defer c.Cleanup()
  7134  
  7135  	stk := c.Stack()
  7136  	// Set lower limits for auto-tuning tests. This is required because the
  7137  	// test stops the worker which can cause packets to be dropped because
  7138  	// the segment queue holding unprocessed packets is limited to 500.
  7139  	const receiveBufferSize = 80 << 10 // 80KB.
  7140  	const maxReceiveBufferSize = receiveBufferSize * 10
  7141  	{
  7142  		opt := tcpip.TCPReceiveBufferSizeRangeOption{Min: 1, Default: receiveBufferSize, Max: maxReceiveBufferSize}
  7143  		if err := stk.SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  7144  			t.Fatalf("SetTransportProtocolOption(%d, &%#v): %s", tcp.ProtocolNumber, opt, err)
  7145  		}
  7146  	}
  7147  
  7148  	// Enable auto-tuning.
  7149  	{
  7150  		opt := tcpip.TCPModerateReceiveBufferOption(true)
  7151  		if err := stk.SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  7152  			t.Fatalf("SetTransportProtocolOption(%d, &%T(%t)): %s", tcp.ProtocolNumber, opt, opt, err)
  7153  		}
  7154  	}
  7155  	// Change the expected window scale to match the value needed for the
  7156  	// maximum buffer size defined above.
  7157  	c.WindowScale = uint8(tcp.FindWndScale(maxReceiveBufferSize))
  7158  
  7159  	rawEP := c.CreateConnectedWithOptionsNoDelay(header.TCPSynOptions{TS: true, WS: 4})
  7160  
  7161  	// NOTE: The timestamp values in the sent packets are meaningless to the
  7162  	// peer so we just increment the timestamp value by 1 every batch as we
  7163  	// are not really using them for anything. Send a single byte to verify
  7164  	// the advertised window.
  7165  	tsVal := rawEP.TSVal + 1
  7166  
  7167  	// Introduce a 25ms latency by delaying the first byte.
  7168  	latency := 25 * time.Millisecond
  7169  	time.Sleep(latency)
  7170  	// Send an initial payload with atleast segment overhead size. The receive
  7171  	// window would not grow for smaller segments.
  7172  	rawEP.SendPacketWithTS(make([]byte, tcp.SegOverheadSize), tsVal)
  7173  
  7174  	pkt := rawEP.VerifyAndReturnACKWithTS(tsVal)
  7175  	defer pkt.Release()
  7176  	rcvWnd := header.TCP(header.IPv4(pkt.AsSlice()).Payload()).WindowSize()
  7177  
  7178  	time.Sleep(25 * time.Millisecond)
  7179  
  7180  	// Allocate a large enough payload for the test.
  7181  	payloadSize := receiveBufferSize * 2
  7182  	b := make([]byte, payloadSize)
  7183  
  7184  	worker := (c.EP).(interface {
  7185  		StopWork()
  7186  		ResumeWork()
  7187  	})
  7188  	tsVal++
  7189  
  7190  	// Stop the worker goroutine.
  7191  	worker.StopWork()
  7192  	start := 0
  7193  	end := payloadSize / 2
  7194  	packetsSent := 0
  7195  	for ; start < end; start += mss {
  7196  		packetEnd := start + mss
  7197  		if start+mss > end {
  7198  			packetEnd = end
  7199  		}
  7200  		rawEP.SendPacketWithTS(b[start:packetEnd], tsVal)
  7201  		packetsSent++
  7202  	}
  7203  
  7204  	// Resume the worker so that it only sees the packets once all of them
  7205  	// are waiting to be read.
  7206  	worker.ResumeWork()
  7207  
  7208  	// Since we sent almost the full receive buffer worth of data (some may have
  7209  	// been dropped due to segment overheads), we should get a zero window back.
  7210  	pkt = c.GetPacket()
  7211  	defer pkt.Release()
  7212  	tcpHdr := header.TCP(header.IPv4(pkt.AsSlice()).Payload())
  7213  	gotRcvWnd := tcpHdr.WindowSize()
  7214  	wantAckNum := tcpHdr.AckNumber()
  7215  	if got, want := int(gotRcvWnd), 0; got != want {
  7216  		t.Fatalf("got rcvWnd: %d, want: %d", got, want)
  7217  	}
  7218  
  7219  	time.Sleep(25 * time.Millisecond)
  7220  	// Verify that sending more data when receiveBuffer is exhausted.
  7221  	rawEP.SendPacketWithTS(b[start:start+mss], tsVal)
  7222  
  7223  	// Now read all the data from the endpoint and verify that advertised
  7224  	// window increases to the full available buffer size.
  7225  	for {
  7226  		_, err := c.EP.Read(ioutil.Discard, tcpip.ReadOptions{})
  7227  		if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  7228  			break
  7229  		}
  7230  	}
  7231  
  7232  	// Verify that we receive a non-zero window update ACK. When running
  7233  	// under thread sanitizer this test can end up sending more than 1
  7234  	// ack, 1 for the non-zero window
  7235  	p := c.GetPacket()
  7236  	defer p.Release()
  7237  	checker.IPv4(t, p, checker.TCP(
  7238  		checker.TCPAckNum(wantAckNum),
  7239  		func(t *testing.T, h header.Transport) {
  7240  			tcp, ok := h.(header.TCP)
  7241  			if !ok {
  7242  				return
  7243  			}
  7244  			// We use 10% here as the error margin upwards as the initial window we
  7245  			// got was after 1 segment was already in the receive buffer queue.
  7246  			tolerance := 1.1
  7247  			if w := tcp.WindowSize(); w == 0 || w > uint16(float64(rcvWnd)*tolerance) {
  7248  				t.Errorf("expected a non-zero window: got %d, want <= %d", w, uint16(float64(rcvWnd)*tolerance))
  7249  			}
  7250  		},
  7251  	))
  7252  }
  7253  
  7254  // This test verifies that the advertised window is auto-tuned up as the
  7255  // application is reading the data that is being received.
  7256  func TestReceiveBufferAutoTuning(t *testing.T) {
  7257  	const mtu = 1500
  7258  	const mss = mtu - header.IPv4MinimumSize - header.TCPMinimumSize
  7259  
  7260  	c := context.New(t, mtu)
  7261  	defer c.Cleanup()
  7262  
  7263  	// Enable Auto-tuning.
  7264  	stk := c.Stack()
  7265  	// Disable out of window rate limiting for this test by setting it to 0 as we
  7266  	// use out of window ACKs to measure the advertised window.
  7267  	var tcpInvalidRateLimit stack.TCPInvalidRateLimitOption
  7268  	if err := stk.SetOption(tcpInvalidRateLimit); err != nil {
  7269  		t.Fatalf("e.stack.SetOption(%#v) = %s", tcpInvalidRateLimit, err)
  7270  	}
  7271  
  7272  	const receiveBufferSize = 80 << 10 // 80KB.
  7273  	const maxReceiveBufferSize = receiveBufferSize * 10
  7274  	{
  7275  		opt := tcpip.TCPReceiveBufferSizeRangeOption{Min: 1, Default: receiveBufferSize, Max: maxReceiveBufferSize}
  7276  		if err := stk.SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  7277  			t.Fatalf("SetTransportProtocolOption(%d, &%#v): %s", tcp.ProtocolNumber, opt, err)
  7278  		}
  7279  	}
  7280  
  7281  	// Enable auto-tuning.
  7282  	{
  7283  		opt := tcpip.TCPModerateReceiveBufferOption(true)
  7284  		if err := stk.SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  7285  			t.Fatalf("SetTransportProtocolOption(%d, &%T(%t)): %s", tcp.ProtocolNumber, opt, opt, err)
  7286  		}
  7287  	}
  7288  	// Change the expected window scale to match the value needed for the
  7289  	// maximum buffer size used by stack.
  7290  	c.WindowScale = uint8(tcp.FindWndScale(maxReceiveBufferSize))
  7291  
  7292  	rawEP := c.CreateConnectedWithOptionsNoDelay(header.TCPSynOptions{TS: true, WS: 4})
  7293  	tsVal := rawEP.TSVal
  7294  	rawEP.NextSeqNum--
  7295  	rawEP.SendPacketWithTS(nil, tsVal)
  7296  	rawEP.NextSeqNum++
  7297  	pkt := rawEP.VerifyAndReturnACKWithTS(tsVal)
  7298  	defer pkt.Release()
  7299  	curRcvWnd := int(header.TCP(header.IPv4(pkt.AsSlice()).Payload()).WindowSize()) << c.WindowScale
  7300  	scaleRcvWnd := func(rcvWnd int) uint16 {
  7301  		return uint16(rcvWnd >> c.WindowScale)
  7302  	}
  7303  	// Allocate a large array to send to the endpoint.
  7304  	b := make([]byte, receiveBufferSize*48)
  7305  
  7306  	// In every iteration we will send double the number of bytes sent in
  7307  	// the previous iteration and read the same from the app. The received
  7308  	// window should grow by at least 2x of bytes read by the app in every
  7309  	// RTT.
  7310  	offset := 0
  7311  	payloadSize := receiveBufferSize / 8
  7312  	worker := (c.EP).(interface {
  7313  		StopWork()
  7314  		ResumeWork()
  7315  	})
  7316  	latency := 1 * time.Millisecond
  7317  	for i := 0; i < 5; i++ {
  7318  		tsVal++
  7319  
  7320  		// Stop the worker goroutine.
  7321  		worker.StopWork()
  7322  		start := offset
  7323  		end := offset + payloadSize
  7324  		totalSent := 0
  7325  		packetsSent := 0
  7326  		for ; start < end; start += mss {
  7327  			rawEP.SendPacketWithTS(b[start:start+mss], tsVal)
  7328  			totalSent += mss
  7329  			packetsSent++
  7330  		}
  7331  
  7332  		// Resume it so that it only sees the packets once all of them
  7333  		// are waiting to be read.
  7334  		worker.ResumeWork()
  7335  
  7336  		// Give 1ms for the worker to process the packets.
  7337  		time.Sleep(1 * time.Millisecond)
  7338  
  7339  		lastACK := c.GetPacket()
  7340  		defer lastACK.Release()
  7341  		// Discard any intermediate ACKs and only check the last ACK we get in a
  7342  		// short time period of few ms.
  7343  		for {
  7344  			time.Sleep(1 * time.Millisecond)
  7345  			pkt := c.GetPacketNonBlocking()
  7346  			if pkt == nil {
  7347  				break
  7348  			}
  7349  			defer pkt.Release()
  7350  			lastACK = pkt
  7351  		}
  7352  		if got, want := int(header.TCP(header.IPv4(lastACK.AsSlice()).Payload()).WindowSize()), int(scaleRcvWnd(curRcvWnd)); got > want {
  7353  			t.Fatalf("advertised window got: %d, want <= %d", got, want)
  7354  		}
  7355  
  7356  		// Now read all the data from the endpoint and invoke the
  7357  		// moderation API to allow for receive buffer auto-tuning
  7358  		// to happen before we measure the new window.
  7359  		totalCopied := 0
  7360  		for {
  7361  			res, err := c.EP.Read(ioutil.Discard, tcpip.ReadOptions{})
  7362  			if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  7363  				break
  7364  			}
  7365  			totalCopied += res.Count
  7366  		}
  7367  
  7368  		// Invoke the moderation API. This is required for auto-tuning
  7369  		// to happen. This method is normally expected to be invoked
  7370  		// from a higher layer than tcpip.Endpoint. So we simulate
  7371  		// copying to userspace by invoking it explicitly here.
  7372  		c.EP.ModerateRecvBuf(totalCopied)
  7373  
  7374  		// Now send a keep-alive packet to trigger an ACK so that we can
  7375  		// measure the new window.
  7376  		rawEP.NextSeqNum--
  7377  		rawEP.SendPacketWithTS(nil, tsVal)
  7378  		rawEP.NextSeqNum++
  7379  
  7380  		if i == 0 {
  7381  			// In the first iteration the receiver based RTT is not
  7382  			// yet known as a result the moderation code should not
  7383  			// increase the advertised window.
  7384  			rawEP.VerifyACKRcvWnd(scaleRcvWnd(curRcvWnd))
  7385  		} else {
  7386  			// Read loop above could generate an ACK if the window had dropped to
  7387  			// zero and then read had opened it up.
  7388  			lastACK := c.GetPacket()
  7389  			defer lastACK.Release()
  7390  			// Discard any intermediate ACKs and only check the last ACK we get in a
  7391  			// short time period of few ms.
  7392  			for {
  7393  				time.Sleep(1 * time.Millisecond)
  7394  				pkt := c.GetPacketNonBlocking()
  7395  				if pkt == nil {
  7396  					break
  7397  				}
  7398  				defer pkt.Release()
  7399  				lastACK = pkt
  7400  			}
  7401  			curRcvWnd = int(header.TCP(header.IPv4(lastACK.AsSlice()).Payload()).WindowSize()) << c.WindowScale
  7402  			// If thew new current window is close maxReceiveBufferSize then terminate
  7403  			// the loop. This can happen before all iterations are done due to timing
  7404  			// differences when running the test.
  7405  			if int(float64(curRcvWnd)*1.1) > maxReceiveBufferSize/2 {
  7406  				break
  7407  			}
  7408  			// Increase the latency after first two iterations to
  7409  			// establish a low RTT value in the receiver since it
  7410  			// only tracks the lowest value. This ensures that when
  7411  			// ModerateRcvBuf is called the elapsed time is always >
  7412  			// rtt. Without this the test is flaky due to delays due
  7413  			// to scheduling/wakeup etc.
  7414  			latency += 50 * time.Millisecond
  7415  		}
  7416  		time.Sleep(latency)
  7417  		offset += payloadSize
  7418  		payloadSize *= 2
  7419  	}
  7420  	// Check that at the end of our iterations the receive window grew close to the maximum
  7421  	// permissible size of maxReceiveBufferSize/2
  7422  	if got, want := int(float64(curRcvWnd)*1.1), maxReceiveBufferSize/2; got < want {
  7423  		t.Fatalf("unexpected rcvWnd got: %d, want > %d", got, want)
  7424  	}
  7425  
  7426  }
  7427  
  7428  func TestDelayEnabled(t *testing.T) {
  7429  	c := context.New(t, e2e.DefaultMTU)
  7430  	defer c.Cleanup()
  7431  	checkDelayOption(t, c, false, false) // Delay is disabled by default.
  7432  
  7433  	for _, delayEnabled := range []bool{false, true} {
  7434  		t.Run(fmt.Sprintf("delayEnabled=%t", delayEnabled), func(t *testing.T) {
  7435  			c := context.New(t, e2e.DefaultMTU)
  7436  			defer c.Cleanup()
  7437  			opt := tcpip.TCPDelayEnabled(delayEnabled)
  7438  			if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  7439  				t.Fatalf("SetTransportProtocolOption(%d, &%T(%t)): %s", tcp.ProtocolNumber, opt, delayEnabled, err)
  7440  			}
  7441  			checkDelayOption(t, c, opt, delayEnabled)
  7442  		})
  7443  	}
  7444  }
  7445  
  7446  func checkDelayOption(t *testing.T, c *context.Context, wantDelayEnabled tcpip.TCPDelayEnabled, wantDelayOption bool) {
  7447  	t.Helper()
  7448  
  7449  	var gotDelayEnabled tcpip.TCPDelayEnabled
  7450  	if err := c.Stack().TransportProtocolOption(tcp.ProtocolNumber, &gotDelayEnabled); err != nil {
  7451  		t.Fatalf("TransportProtocolOption(tcp, &gotDelayEnabled) failed: %s", err)
  7452  	}
  7453  	if gotDelayEnabled != wantDelayEnabled {
  7454  		t.Errorf("TransportProtocolOption(tcp, &gotDelayEnabled) got %t, want %t", gotDelayEnabled, wantDelayEnabled)
  7455  	}
  7456  
  7457  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, new(waiter.Queue))
  7458  	if err != nil {
  7459  		t.Fatalf("NewEndPoint(tcp, ipv4, new(waiter.Queue)) failed: %s", err)
  7460  	}
  7461  	gotDelayOption := ep.SocketOptions().GetDelayOption()
  7462  	if gotDelayOption != wantDelayOption {
  7463  		t.Errorf("ep.GetSockOptBool(tcpip.DelayOption) got: %t, want: %t", gotDelayOption, wantDelayOption)
  7464  	}
  7465  }
  7466  
  7467  func TestTCPLingerTimeout(t *testing.T) {
  7468  	c := context.New(t, 1500 /* mtu */)
  7469  	defer c.Cleanup()
  7470  
  7471  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  7472  
  7473  	testCases := []struct {
  7474  		name             string
  7475  		tcpLingerTimeout time.Duration
  7476  		want             time.Duration
  7477  	}{
  7478  		{"NegativeLingerTimeout", -123123, -1},
  7479  		// Zero is treated same as the stack's default TCP_LINGER2 timeout.
  7480  		{"ZeroLingerTimeout", 0, tcp.DefaultTCPLingerTimeout},
  7481  		{"InRangeLingerTimeout", 10 * time.Second, 10 * time.Second},
  7482  		// Values > stack's TCPLingerTimeout are capped to the stack's
  7483  		// value. Defaults to tcp.DefaultTCPLingerTimeout(60 seconds)
  7484  		{"AboveMaxLingerTimeout", tcp.MaxTCPLingerTimeout + 5*time.Second, tcp.MaxTCPLingerTimeout},
  7485  	}
  7486  	for _, tc := range testCases {
  7487  		t.Run(tc.name, func(t *testing.T) {
  7488  			v := tcpip.TCPLingerTimeoutOption(tc.tcpLingerTimeout)
  7489  			if err := c.EP.SetSockOpt(&v); err != nil {
  7490  				t.Fatalf("SetSockOpt(&%T(%s)) = %s", v, tc.tcpLingerTimeout, err)
  7491  			}
  7492  
  7493  			v = 0
  7494  			if err := c.EP.GetSockOpt(&v); err != nil {
  7495  				t.Fatalf("GetSockOpt(&%T) = %s", v, err)
  7496  			}
  7497  			if got, want := time.Duration(v), tc.want; got != want {
  7498  				t.Fatalf("got linger timeout = %s, want = %s", got, want)
  7499  			}
  7500  		})
  7501  	}
  7502  }
  7503  
  7504  func TestTCPTimeWaitRSTIgnored(t *testing.T) {
  7505  	c := context.New(t, e2e.DefaultMTU)
  7506  	defer c.Cleanup()
  7507  
  7508  	wq := &waiter.Queue{}
  7509  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  7510  	if err != nil {
  7511  		t.Fatalf("NewEndpoint failed: %s", err)
  7512  	}
  7513  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  7514  		t.Fatalf("Bind failed: %s", err)
  7515  	}
  7516  
  7517  	if err := ep.Listen(10); err != nil {
  7518  		t.Fatalf("Listen failed: %s", err)
  7519  	}
  7520  
  7521  	// Send a SYN request.
  7522  	iss := seqnum.Value(context.TestInitialSequenceNumber)
  7523  	c.SendPacket(nil, &context.Headers{
  7524  		SrcPort: context.TestPort,
  7525  		DstPort: context.StackPort,
  7526  		Flags:   header.TCPFlagSyn,
  7527  		SeqNum:  iss,
  7528  		RcvWnd:  30000,
  7529  	})
  7530  
  7531  	// Receive the SYN-ACK reply.
  7532  	b := c.GetPacket()
  7533  	defer b.Release()
  7534  	tcpHdr := header.TCP(header.IPv4(b.AsSlice()).Payload())
  7535  	c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
  7536  
  7537  	ackHeaders := &context.Headers{
  7538  		SrcPort: context.TestPort,
  7539  		DstPort: context.StackPort,
  7540  		Flags:   header.TCPFlagAck,
  7541  		SeqNum:  iss + 1,
  7542  		AckNum:  c.IRS + 1,
  7543  	}
  7544  
  7545  	// Send ACK.
  7546  	c.SendPacket(nil, ackHeaders)
  7547  
  7548  	// Try to accept the connection.
  7549  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  7550  	wq.EventRegister(&we)
  7551  	defer wq.EventUnregister(&we)
  7552  
  7553  	c.EP, _, err = ep.Accept(nil)
  7554  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  7555  		// Wait for connection to be established.
  7556  		select {
  7557  		case <-ch:
  7558  			c.EP, _, err = ep.Accept(nil)
  7559  			if err != nil {
  7560  				t.Fatalf("Accept failed: %s", err)
  7561  			}
  7562  
  7563  		case <-time.After(1 * time.Second):
  7564  			t.Fatalf("Timed out waiting for accept")
  7565  		}
  7566  	}
  7567  
  7568  	c.EP.Close()
  7569  
  7570  	v := c.GetPacket()
  7571  	defer v.Release()
  7572  	checker.IPv4(t, v, checker.TCP(
  7573  		checker.SrcPort(context.StackPort),
  7574  		checker.DstPort(context.TestPort),
  7575  		checker.TCPSeqNum(uint32(c.IRS+1)),
  7576  		checker.TCPAckNum(uint32(iss)+1),
  7577  		checker.TCPFlags(header.TCPFlagFin|header.TCPFlagAck)))
  7578  
  7579  	finHeaders := &context.Headers{
  7580  		SrcPort: context.TestPort,
  7581  		DstPort: context.StackPort,
  7582  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  7583  		SeqNum:  iss + 1,
  7584  		AckNum:  c.IRS + 2,
  7585  	}
  7586  
  7587  	c.SendPacket(nil, finHeaders)
  7588  
  7589  	// Get the ACK to the FIN we just sent.
  7590  
  7591  	v = c.GetPacket()
  7592  	defer v.Release()
  7593  	checker.IPv4(t, v, checker.TCP(
  7594  		checker.SrcPort(context.StackPort),
  7595  		checker.DstPort(context.TestPort),
  7596  		checker.TCPSeqNum(uint32(c.IRS+2)),
  7597  		checker.TCPAckNum(uint32(iss)+2),
  7598  		checker.TCPFlags(header.TCPFlagAck)))
  7599  
  7600  	// Now send a RST and this should be ignored and not
  7601  	// generate an ACK.
  7602  	c.SendPacket(nil, &context.Headers{
  7603  		SrcPort: context.TestPort,
  7604  		DstPort: context.StackPort,
  7605  		Flags:   header.TCPFlagRst,
  7606  		SeqNum:  iss + 1,
  7607  		AckNum:  c.IRS + 2,
  7608  	})
  7609  
  7610  	c.CheckNoPacketTimeout("unexpected packet received in TIME_WAIT state", 1*time.Second)
  7611  
  7612  	// Out of order ACK should generate an immediate ACK in
  7613  	// TIME_WAIT.
  7614  	c.SendPacket(nil, &context.Headers{
  7615  		SrcPort: context.TestPort,
  7616  		DstPort: context.StackPort,
  7617  		Flags:   header.TCPFlagAck,
  7618  		SeqNum:  iss + 1,
  7619  		AckNum:  c.IRS + 3,
  7620  	})
  7621  
  7622  	v = c.GetPacket()
  7623  	defer v.Release()
  7624  	checker.IPv4(t, v, checker.TCP(
  7625  		checker.SrcPort(context.StackPort),
  7626  		checker.DstPort(context.TestPort),
  7627  		checker.TCPSeqNum(uint32(c.IRS+2)),
  7628  		checker.TCPAckNum(uint32(iss)+2),
  7629  		checker.TCPFlags(header.TCPFlagAck)))
  7630  }
  7631  
  7632  func TestTCPTimeWaitOutOfOrder(t *testing.T) {
  7633  	c := context.New(t, e2e.DefaultMTU)
  7634  	defer c.Cleanup()
  7635  
  7636  	wq := &waiter.Queue{}
  7637  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  7638  	if err != nil {
  7639  		t.Fatalf("NewEndpoint failed: %s", err)
  7640  	}
  7641  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  7642  		t.Fatalf("Bind failed: %s", err)
  7643  	}
  7644  
  7645  	if err := ep.Listen(10); err != nil {
  7646  		t.Fatalf("Listen failed: %s", err)
  7647  	}
  7648  
  7649  	// Send a SYN request.
  7650  	iss := seqnum.Value(context.TestInitialSequenceNumber)
  7651  	c.SendPacket(nil, &context.Headers{
  7652  		SrcPort: context.TestPort,
  7653  		DstPort: context.StackPort,
  7654  		Flags:   header.TCPFlagSyn,
  7655  		SeqNum:  iss,
  7656  		RcvWnd:  30000,
  7657  	})
  7658  
  7659  	// Receive the SYN-ACK reply.
  7660  	b := c.GetPacket()
  7661  	defer b.Release()
  7662  	tcpHdr := header.TCP(header.IPv4(b.AsSlice()).Payload())
  7663  	c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
  7664  
  7665  	ackHeaders := &context.Headers{
  7666  		SrcPort: context.TestPort,
  7667  		DstPort: context.StackPort,
  7668  		Flags:   header.TCPFlagAck,
  7669  		SeqNum:  iss + 1,
  7670  		AckNum:  c.IRS + 1,
  7671  	}
  7672  
  7673  	// Send ACK.
  7674  	c.SendPacket(nil, ackHeaders)
  7675  
  7676  	// Try to accept the connection.
  7677  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  7678  	wq.EventRegister(&we)
  7679  	defer wq.EventUnregister(&we)
  7680  
  7681  	c.EP, _, err = ep.Accept(nil)
  7682  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  7683  		// Wait for connection to be established.
  7684  		select {
  7685  		case <-ch:
  7686  			c.EP, _, err = ep.Accept(nil)
  7687  			if err != nil {
  7688  				t.Fatalf("Accept failed: %s", err)
  7689  			}
  7690  
  7691  		case <-time.After(1 * time.Second):
  7692  			t.Fatalf("Timed out waiting for accept")
  7693  		}
  7694  	}
  7695  
  7696  	c.EP.Close()
  7697  
  7698  	v := c.GetPacket()
  7699  	defer v.Release()
  7700  	checker.IPv4(t, v, checker.TCP(
  7701  		checker.SrcPort(context.StackPort),
  7702  		checker.DstPort(context.TestPort),
  7703  		checker.TCPSeqNum(uint32(c.IRS+1)),
  7704  		checker.TCPAckNum(uint32(iss)+1),
  7705  		checker.TCPFlags(header.TCPFlagFin|header.TCPFlagAck)))
  7706  
  7707  	finHeaders := &context.Headers{
  7708  		SrcPort: context.TestPort,
  7709  		DstPort: context.StackPort,
  7710  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  7711  		SeqNum:  iss + 1,
  7712  		AckNum:  c.IRS + 2,
  7713  	}
  7714  
  7715  	c.SendPacket(nil, finHeaders)
  7716  
  7717  	// Get the ACK to the FIN we just sent.
  7718  	v = c.GetPacket()
  7719  	defer v.Release()
  7720  	checker.IPv4(t, v, checker.TCP(
  7721  		checker.SrcPort(context.StackPort),
  7722  		checker.DstPort(context.TestPort),
  7723  		checker.TCPSeqNum(uint32(c.IRS+2)),
  7724  		checker.TCPAckNum(uint32(iss)+2),
  7725  		checker.TCPFlags(header.TCPFlagAck)))
  7726  
  7727  	// Out of order ACK should generate an immediate ACK in
  7728  	// TIME_WAIT.
  7729  	c.SendPacket(nil, &context.Headers{
  7730  		SrcPort: context.TestPort,
  7731  		DstPort: context.StackPort,
  7732  		Flags:   header.TCPFlagAck,
  7733  		SeqNum:  iss + 1,
  7734  		AckNum:  c.IRS + 3,
  7735  	})
  7736  
  7737  	v = c.GetPacket()
  7738  	defer v.Release()
  7739  	checker.IPv4(t, v, checker.TCP(
  7740  		checker.SrcPort(context.StackPort),
  7741  		checker.DstPort(context.TestPort),
  7742  		checker.TCPSeqNum(uint32(c.IRS+2)),
  7743  		checker.TCPAckNum(uint32(iss)+2),
  7744  		checker.TCPFlags(header.TCPFlagAck)))
  7745  }
  7746  
  7747  func TestTCPTimeWaitNewSyn(t *testing.T) {
  7748  	c := context.New(t, e2e.DefaultMTU)
  7749  	defer c.Cleanup()
  7750  
  7751  	wq := &waiter.Queue{}
  7752  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  7753  	if err != nil {
  7754  		t.Fatalf("NewEndpoint failed: %s", err)
  7755  	}
  7756  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  7757  		t.Fatalf("Bind failed: %s", err)
  7758  	}
  7759  
  7760  	if err := ep.Listen(10); err != nil {
  7761  		t.Fatalf("Listen failed: %s", err)
  7762  	}
  7763  
  7764  	// Send a SYN request.
  7765  	iss := seqnum.Value(context.TestInitialSequenceNumber)
  7766  	c.SendPacket(nil, &context.Headers{
  7767  		SrcPort: context.TestPort,
  7768  		DstPort: context.StackPort,
  7769  		Flags:   header.TCPFlagSyn,
  7770  		SeqNum:  iss,
  7771  		RcvWnd:  30000,
  7772  	})
  7773  
  7774  	// Receive the SYN-ACK reply.
  7775  	b := c.GetPacket()
  7776  	defer b.Release()
  7777  	tcpHdr := header.TCP(header.IPv4(b.AsSlice()).Payload())
  7778  	c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
  7779  
  7780  	ackHeaders := &context.Headers{
  7781  		SrcPort: context.TestPort,
  7782  		DstPort: context.StackPort,
  7783  		Flags:   header.TCPFlagAck,
  7784  		SeqNum:  iss + 1,
  7785  		AckNum:  c.IRS + 1,
  7786  	}
  7787  
  7788  	// Send ACK.
  7789  	c.SendPacket(nil, ackHeaders)
  7790  
  7791  	// Try to accept the connection.
  7792  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  7793  	wq.EventRegister(&we)
  7794  	defer wq.EventUnregister(&we)
  7795  
  7796  	c.EP, _, err = ep.Accept(nil)
  7797  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  7798  		// Wait for connection to be established.
  7799  		select {
  7800  		case <-ch:
  7801  			c.EP, _, err = ep.Accept(nil)
  7802  			if err != nil {
  7803  				t.Fatalf("Accept failed: %s", err)
  7804  			}
  7805  
  7806  		case <-time.After(1 * time.Second):
  7807  			t.Fatalf("Timed out waiting for accept")
  7808  		}
  7809  	}
  7810  
  7811  	c.EP.Close()
  7812  
  7813  	v := c.GetPacket()
  7814  	defer v.Release()
  7815  	checker.IPv4(t, v, checker.TCP(
  7816  		checker.SrcPort(context.StackPort),
  7817  		checker.DstPort(context.TestPort),
  7818  		checker.TCPSeqNum(uint32(c.IRS+1)),
  7819  		checker.TCPAckNum(uint32(iss)+1),
  7820  		checker.TCPFlags(header.TCPFlagFin|header.TCPFlagAck)))
  7821  
  7822  	finHeaders := &context.Headers{
  7823  		SrcPort: context.TestPort,
  7824  		DstPort: context.StackPort,
  7825  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  7826  		SeqNum:  iss + 1,
  7827  		AckNum:  c.IRS + 2,
  7828  	}
  7829  
  7830  	c.SendPacket(nil, finHeaders)
  7831  
  7832  	// Get the ACK to the FIN we just sent.
  7833  	v = c.GetPacket()
  7834  	defer v.Release()
  7835  	checker.IPv4(t, v, checker.TCP(
  7836  		checker.SrcPort(context.StackPort),
  7837  		checker.DstPort(context.TestPort),
  7838  		checker.TCPSeqNum(uint32(c.IRS+2)),
  7839  		checker.TCPAckNum(uint32(iss)+2),
  7840  		checker.TCPFlags(header.TCPFlagAck)))
  7841  
  7842  	// Send a SYN request w/ sequence number lower than
  7843  	// the highest sequence number sent. We just reuse
  7844  	// the same number.
  7845  	iss = seqnum.Value(context.TestInitialSequenceNumber)
  7846  	c.SendPacket(nil, &context.Headers{
  7847  		SrcPort: context.TestPort,
  7848  		DstPort: context.StackPort,
  7849  		Flags:   header.TCPFlagSyn,
  7850  		SeqNum:  iss,
  7851  		RcvWnd:  30000,
  7852  	})
  7853  
  7854  	c.CheckNoPacketTimeout("unexpected packet received in response to SYN", 1*time.Second)
  7855  
  7856  	// drain any older notifications from the notification channel before attempting
  7857  	// 2nd connection.
  7858  	select {
  7859  	case <-ch:
  7860  	default:
  7861  	}
  7862  
  7863  	// Send a SYN request w/ sequence number higher than
  7864  	// the highest sequence number sent.
  7865  	iss = iss.Add(3)
  7866  	c.SendPacket(nil, &context.Headers{
  7867  		SrcPort: context.TestPort,
  7868  		DstPort: context.StackPort,
  7869  		Flags:   header.TCPFlagSyn,
  7870  		SeqNum:  iss,
  7871  		RcvWnd:  30000,
  7872  	})
  7873  
  7874  	// Receive the SYN-ACK reply.
  7875  	b = c.GetPacket()
  7876  	defer b.Release()
  7877  	tcpHdr = header.IPv4(b.AsSlice()).Payload()
  7878  	c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
  7879  
  7880  	ackHeaders = &context.Headers{
  7881  		SrcPort: context.TestPort,
  7882  		DstPort: context.StackPort,
  7883  		Flags:   header.TCPFlagAck,
  7884  		SeqNum:  iss + 1,
  7885  		AckNum:  c.IRS + 1,
  7886  	}
  7887  
  7888  	// Send ACK.
  7889  	c.SendPacket(nil, ackHeaders)
  7890  
  7891  	// Try to accept the connection.
  7892  	c.EP, _, err = ep.Accept(nil)
  7893  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  7894  		// Wait for connection to be established.
  7895  		select {
  7896  		case <-ch:
  7897  			c.EP, _, err = ep.Accept(nil)
  7898  			if err != nil {
  7899  				t.Fatalf("Accept failed: %s", err)
  7900  			}
  7901  
  7902  		case <-time.After(1 * time.Second):
  7903  			t.Fatalf("Timed out waiting for accept")
  7904  		}
  7905  	}
  7906  }
  7907  
  7908  func TestTCPTimeWaitDuplicateFINExtendsTimeWait(t *testing.T) {
  7909  	c := context.New(t, e2e.DefaultMTU)
  7910  	defer c.Cleanup()
  7911  
  7912  	// Set TCPTimeWaitTimeout to 5 seconds so that sockets are marked closed
  7913  	// after 5 seconds in TIME_WAIT state.
  7914  	tcpTimeWaitTimeout := 5 * time.Second
  7915  	opt := tcpip.TCPTimeWaitTimeoutOption(tcpTimeWaitTimeout)
  7916  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  7917  		t.Fatalf("SetTransportProtocolOption(%d, &%T(%s)): %s", tcp.ProtocolNumber, opt, tcpTimeWaitTimeout, err)
  7918  	}
  7919  
  7920  	want := c.Stack().Stats().TCP.EstablishedClosed.Value() + 1
  7921  
  7922  	wq := &waiter.Queue{}
  7923  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  7924  	if err != nil {
  7925  		t.Fatalf("NewEndpoint failed: %s", err)
  7926  	}
  7927  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  7928  		t.Fatalf("Bind failed: %s", err)
  7929  	}
  7930  
  7931  	if err := ep.Listen(10); err != nil {
  7932  		t.Fatalf("Listen failed: %s", err)
  7933  	}
  7934  
  7935  	// Send a SYN request.
  7936  	iss := seqnum.Value(context.TestInitialSequenceNumber)
  7937  	c.SendPacket(nil, &context.Headers{
  7938  		SrcPort: context.TestPort,
  7939  		DstPort: context.StackPort,
  7940  		Flags:   header.TCPFlagSyn,
  7941  		SeqNum:  iss,
  7942  		RcvWnd:  30000,
  7943  	})
  7944  
  7945  	// Receive the SYN-ACK reply.
  7946  	b := c.GetPacket()
  7947  	defer b.Release()
  7948  	tcpHdr := header.TCP(header.IPv4(b.AsSlice()).Payload())
  7949  	c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
  7950  
  7951  	ackHeaders := &context.Headers{
  7952  		SrcPort: context.TestPort,
  7953  		DstPort: context.StackPort,
  7954  		Flags:   header.TCPFlagAck,
  7955  		SeqNum:  iss + 1,
  7956  		AckNum:  c.IRS + 1,
  7957  	}
  7958  
  7959  	// Send ACK.
  7960  	c.SendPacket(nil, ackHeaders)
  7961  
  7962  	// Try to accept the connection.
  7963  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  7964  	wq.EventRegister(&we)
  7965  	defer wq.EventUnregister(&we)
  7966  
  7967  	c.EP, _, err = ep.Accept(nil)
  7968  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  7969  		// Wait for connection to be established.
  7970  		select {
  7971  		case <-ch:
  7972  			c.EP, _, err = ep.Accept(nil)
  7973  			if err != nil {
  7974  				t.Fatalf("Accept failed: %s", err)
  7975  			}
  7976  
  7977  		case <-time.After(1 * time.Second):
  7978  			t.Fatalf("Timed out waiting for accept")
  7979  		}
  7980  	}
  7981  
  7982  	c.EP.Close()
  7983  	v := c.GetPacket()
  7984  	defer v.Release()
  7985  	checker.IPv4(t, v, checker.TCP(
  7986  		checker.SrcPort(context.StackPort),
  7987  		checker.DstPort(context.TestPort),
  7988  		checker.TCPSeqNum(uint32(c.IRS+1)),
  7989  		checker.TCPAckNum(uint32(iss)+1),
  7990  		checker.TCPFlags(header.TCPFlagFin|header.TCPFlagAck)))
  7991  
  7992  	finHeaders := &context.Headers{
  7993  		SrcPort: context.TestPort,
  7994  		DstPort: context.StackPort,
  7995  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  7996  		SeqNum:  iss + 1,
  7997  		AckNum:  c.IRS + 2,
  7998  	}
  7999  
  8000  	c.SendPacket(nil, finHeaders)
  8001  
  8002  	// Get the ACK to the FIN we just sent.
  8003  	v = c.GetPacket()
  8004  	defer v.Release()
  8005  	checker.IPv4(t, v, checker.TCP(
  8006  		checker.SrcPort(context.StackPort),
  8007  		checker.DstPort(context.TestPort),
  8008  		checker.TCPSeqNum(uint32(c.IRS+2)),
  8009  		checker.TCPAckNum(uint32(iss)+2),
  8010  		checker.TCPFlags(header.TCPFlagAck)))
  8011  
  8012  	time.Sleep(2 * time.Second)
  8013  
  8014  	// Now send a duplicate FIN. This should cause the TIME_WAIT to extend
  8015  	// by another 5 seconds and also send us a duplicate ACK as it should
  8016  	// indicate that the final ACK was potentially lost.
  8017  	c.SendPacket(nil, finHeaders)
  8018  
  8019  	// Get the ACK to the FIN we just sent.
  8020  	v = c.GetPacket()
  8021  	defer v.Release()
  8022  	checker.IPv4(t, v, checker.TCP(
  8023  		checker.SrcPort(context.StackPort),
  8024  		checker.DstPort(context.TestPort),
  8025  		checker.TCPSeqNum(uint32(c.IRS+2)),
  8026  		checker.TCPAckNum(uint32(iss)+2),
  8027  		checker.TCPFlags(header.TCPFlagAck)))
  8028  
  8029  	// Sleep for 4 seconds so at this point we are 1 second past the
  8030  	// original tcpLingerTimeout of 5 seconds.
  8031  	time.Sleep(4 * time.Second)
  8032  
  8033  	// Send an ACK and it should not generate any packet as the socket
  8034  	// should still be in TIME_WAIT for another another 5 seconds due
  8035  	// to the duplicate FIN we sent earlier.
  8036  	*ackHeaders = *finHeaders
  8037  	ackHeaders.SeqNum = ackHeaders.SeqNum + 1
  8038  	ackHeaders.Flags = header.TCPFlagAck
  8039  	c.SendPacket(nil, ackHeaders)
  8040  
  8041  	c.CheckNoPacketTimeout("unexpected packet received from endpoint in TIME_WAIT", 1*time.Second)
  8042  	// Now sleep for another 2 seconds so that we are past the
  8043  	// extended TIME_WAIT of 7 seconds (2 + 5).
  8044  	time.Sleep(2 * time.Second)
  8045  
  8046  	// Resend the same ACK.
  8047  	c.SendPacket(nil, ackHeaders)
  8048  
  8049  	// Receive the RST that should be generated as there is no valid
  8050  	// endpoint.
  8051  	v = c.GetPacket()
  8052  	defer v.Release()
  8053  	checker.IPv4(t, v, checker.TCP(
  8054  		checker.SrcPort(context.StackPort),
  8055  		checker.DstPort(context.TestPort),
  8056  		checker.TCPSeqNum(uint32(ackHeaders.AckNum)),
  8057  		checker.TCPAckNum(0),
  8058  		checker.TCPFlags(header.TCPFlagRst)))
  8059  
  8060  	if got := c.Stack().Stats().TCP.EstablishedClosed.Value(); got != want {
  8061  		t.Errorf("got c.Stack().Stats().TCP.EstablishedClosed = %d, want = %d", got, want)
  8062  	}
  8063  	if got := c.Stack().Stats().TCP.CurrentEstablished.Value(); got != 0 {
  8064  		t.Errorf("got stats.TCP.CurrentEstablished.Value() = %d, want = 0", got)
  8065  	}
  8066  }
  8067  
  8068  func TestTCPCloseWithData(t *testing.T) {
  8069  	c := context.New(t, e2e.DefaultMTU)
  8070  	defer c.Cleanup()
  8071  
  8072  	// Set TCPTimeWaitTimeout to 5 seconds so that sockets are marked closed
  8073  	// after 5 seconds in TIME_WAIT state.
  8074  	tcpTimeWaitTimeout := 5 * time.Second
  8075  	opt := tcpip.TCPTimeWaitTimeoutOption(tcpTimeWaitTimeout)
  8076  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  8077  		t.Fatalf("SetTransportProtocolOption(%d, &%T(%s)): %s", tcp.ProtocolNumber, opt, tcpTimeWaitTimeout, err)
  8078  	}
  8079  
  8080  	wq := &waiter.Queue{}
  8081  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  8082  	if err != nil {
  8083  		t.Fatalf("NewEndpoint failed: %s", err)
  8084  	}
  8085  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  8086  		t.Fatalf("Bind failed: %s", err)
  8087  	}
  8088  
  8089  	if err := ep.Listen(10); err != nil {
  8090  		t.Fatalf("Listen failed: %s", err)
  8091  	}
  8092  
  8093  	// Send a SYN request.
  8094  	iss := seqnum.Value(context.TestInitialSequenceNumber)
  8095  	c.SendPacket(nil, &context.Headers{
  8096  		SrcPort: context.TestPort,
  8097  		DstPort: context.StackPort,
  8098  		Flags:   header.TCPFlagSyn,
  8099  		SeqNum:  iss,
  8100  		RcvWnd:  30000,
  8101  	})
  8102  
  8103  	// Receive the SYN-ACK reply.
  8104  	b := c.GetPacket()
  8105  	defer b.Release()
  8106  	tcpHdr := header.TCP(header.IPv4(b.AsSlice()).Payload())
  8107  	c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
  8108  
  8109  	ackHeaders := &context.Headers{
  8110  		SrcPort: context.TestPort,
  8111  		DstPort: context.StackPort,
  8112  		Flags:   header.TCPFlagAck,
  8113  		SeqNum:  iss + 1,
  8114  		AckNum:  c.IRS + 1,
  8115  		RcvWnd:  30000,
  8116  	}
  8117  
  8118  	// Send ACK.
  8119  	c.SendPacket(nil, ackHeaders)
  8120  
  8121  	// Try to accept the connection.
  8122  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  8123  	wq.EventRegister(&we)
  8124  	defer wq.EventUnregister(&we)
  8125  
  8126  	c.EP, _, err = ep.Accept(nil)
  8127  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  8128  		// Wait for connection to be established.
  8129  		select {
  8130  		case <-ch:
  8131  			c.EP, _, err = ep.Accept(nil)
  8132  			if err != nil {
  8133  				t.Fatalf("Accept failed: %s", err)
  8134  			}
  8135  
  8136  		case <-time.After(1 * time.Second):
  8137  			t.Fatalf("Timed out waiting for accept")
  8138  		}
  8139  	}
  8140  
  8141  	// Now trigger a passive close by sending a FIN.
  8142  	finHeaders := &context.Headers{
  8143  		SrcPort: context.TestPort,
  8144  		DstPort: context.StackPort,
  8145  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  8146  		SeqNum:  iss + 1,
  8147  		AckNum:  c.IRS + 2,
  8148  		RcvWnd:  30000,
  8149  	}
  8150  
  8151  	c.SendPacket(nil, finHeaders)
  8152  
  8153  	// Get the ACK to the FIN we just sent.
  8154  	v := c.GetPacket()
  8155  	defer v.Release()
  8156  	checker.IPv4(t, v, checker.TCP(
  8157  		checker.SrcPort(context.StackPort),
  8158  		checker.DstPort(context.TestPort),
  8159  		checker.TCPSeqNum(uint32(c.IRS+1)),
  8160  		checker.TCPAckNum(uint32(iss)+2),
  8161  		checker.TCPFlags(header.TCPFlagAck)))
  8162  
  8163  	// Now write a few bytes and then close the endpoint.
  8164  	data := []byte{1, 2, 3}
  8165  
  8166  	var r bytes.Reader
  8167  	r.Reset(data)
  8168  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  8169  		t.Fatalf("Write failed: %s", err)
  8170  	}
  8171  
  8172  	// Check that data is received.
  8173  	b = c.GetPacket()
  8174  	defer b.Release()
  8175  	checker.IPv4(t, b,
  8176  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  8177  		checker.TCP(
  8178  			checker.DstPort(context.TestPort),
  8179  			checker.TCPSeqNum(uint32(c.IRS)+1),
  8180  			checker.TCPAckNum(uint32(iss)+2), // Acknum is initial sequence number + 1
  8181  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  8182  		),
  8183  	)
  8184  
  8185  	if p := b.AsSlice()[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(data, p) {
  8186  		t.Errorf("got data = %x, want = %x", p, data)
  8187  	}
  8188  
  8189  	c.EP.Close()
  8190  	// Check the FIN.
  8191  	v = c.GetPacket()
  8192  	defer v.Release()
  8193  	checker.IPv4(t, v, checker.TCP(
  8194  		checker.SrcPort(context.StackPort),
  8195  		checker.DstPort(context.TestPort),
  8196  		checker.TCPSeqNum(uint32(c.IRS+1)+uint32(len(data))),
  8197  		checker.TCPAckNum(uint32(iss+2)),
  8198  		checker.TCPFlags(header.TCPFlagFin|header.TCPFlagAck)))
  8199  
  8200  	// First send a partial ACK.
  8201  	ackHeaders = &context.Headers{
  8202  		SrcPort: context.TestPort,
  8203  		DstPort: context.StackPort,
  8204  		Flags:   header.TCPFlagAck,
  8205  		SeqNum:  iss + 2,
  8206  		AckNum:  c.IRS + 1 + seqnum.Value(len(data)-1),
  8207  		RcvWnd:  30000,
  8208  	}
  8209  	c.SendPacket(nil, ackHeaders)
  8210  
  8211  	// Now send a full ACK.
  8212  	ackHeaders = &context.Headers{
  8213  		SrcPort: context.TestPort,
  8214  		DstPort: context.StackPort,
  8215  		Flags:   header.TCPFlagAck,
  8216  		SeqNum:  iss + 2,
  8217  		AckNum:  c.IRS + 1 + seqnum.Value(len(data)),
  8218  		RcvWnd:  30000,
  8219  	}
  8220  	c.SendPacket(nil, ackHeaders)
  8221  
  8222  	// Now ACK the FIN.
  8223  	ackHeaders.AckNum++
  8224  	c.SendPacket(nil, ackHeaders)
  8225  
  8226  	// Now send an ACK and we should get a RST back as the endpoint should
  8227  	// be in CLOSED state.
  8228  	ackHeaders = &context.Headers{
  8229  		SrcPort: context.TestPort,
  8230  		DstPort: context.StackPort,
  8231  		Flags:   header.TCPFlagAck,
  8232  		SeqNum:  iss + 2,
  8233  		AckNum:  c.IRS + 1 + seqnum.Value(len(data)),
  8234  		RcvWnd:  30000,
  8235  	}
  8236  	c.SendPacket(nil, ackHeaders)
  8237  
  8238  	// Check the RST.
  8239  	v = c.GetPacket()
  8240  	defer v.Release()
  8241  	checker.IPv4(t, v, checker.TCP(
  8242  		checker.SrcPort(context.StackPort),
  8243  		checker.DstPort(context.TestPort),
  8244  		checker.TCPSeqNum(uint32(ackHeaders.AckNum)),
  8245  		checker.TCPAckNum(0),
  8246  		checker.TCPFlags(header.TCPFlagRst)))
  8247  }
  8248  
  8249  func TestTCPUserTimeout(t *testing.T) {
  8250  	c := context.New(t, e2e.DefaultMTU)
  8251  	defer c.Cleanup()
  8252  
  8253  	initRTO := 1 * time.Second
  8254  	minRTOOpt := tcpip.TCPMinRTOOption(initRTO)
  8255  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &minRTOOpt); err != nil {
  8256  		t.Fatalf("SetTransportProtocolOption(%d, &%T(%d)): %s", tcp.ProtocolNumber, minRTOOpt, minRTOOpt, err)
  8257  	}
  8258  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  8259  
  8260  	waitEntry, notifyCh := waiter.NewChannelEntry(waiter.EventHUp)
  8261  	c.WQ.EventRegister(&waitEntry)
  8262  	defer c.WQ.EventUnregister(&waitEntry)
  8263  
  8264  	origEstablishedTimedout := c.Stack().Stats().TCP.EstablishedTimedout.Value()
  8265  
  8266  	// Ensure that on the next retransmit timer fire, the user timeout has
  8267  	// expired.
  8268  	userTimeout := initRTO / 2
  8269  	v := tcpip.TCPUserTimeoutOption(userTimeout)
  8270  	if err := c.EP.SetSockOpt(&v); err != nil {
  8271  		t.Fatalf("c.EP.SetSockOpt(&%T(%s): %s", v, userTimeout, err)
  8272  	}
  8273  
  8274  	// Send some data and wait before ACKing it.
  8275  	view := make([]byte, 3)
  8276  	var r bytes.Reader
  8277  	r.Reset(view)
  8278  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  8279  		t.Fatalf("Write failed: %s", err)
  8280  	}
  8281  
  8282  	next := uint32(c.IRS) + 1
  8283  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  8284  	b := c.GetPacket()
  8285  	defer b.Release()
  8286  	checker.IPv4(t, b,
  8287  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  8288  		checker.TCP(
  8289  			checker.DstPort(context.TestPort),
  8290  			checker.TCPSeqNum(next),
  8291  			checker.TCPAckNum(uint32(iss)),
  8292  			checker.TCPFlagsMatch(header.TCPFlagAck, ^header.TCPFlagPsh),
  8293  		),
  8294  	)
  8295  
  8296  	// Wait for the retransmit timer to be fired and the user timeout to cause
  8297  	// close of the connection.
  8298  	select {
  8299  	case <-notifyCh:
  8300  	case <-time.After(2 * initRTO):
  8301  		t.Fatalf("connection still alive after %s, should have been closed after %s", 2*initRTO, userTimeout)
  8302  	}
  8303  
  8304  	// No packet should be received as the connection should be silently
  8305  	// closed due to timeout.
  8306  	c.CheckNoPacket("unexpected packet received after userTimeout has expired")
  8307  
  8308  	next += uint32(len(view))
  8309  
  8310  	// The connection should be terminated after userTimeout has expired.
  8311  	// Send an ACK to trigger a RST from the stack as the endpoint should
  8312  	// be dead.
  8313  	c.SendPacket(nil, &context.Headers{
  8314  		SrcPort: context.TestPort,
  8315  		DstPort: c.Port,
  8316  		Flags:   header.TCPFlagAck,
  8317  		SeqNum:  iss,
  8318  		AckNum:  seqnum.Value(next),
  8319  		RcvWnd:  30000,
  8320  	})
  8321  
  8322  	b = c.GetPacket()
  8323  	defer b.Release()
  8324  	checker.IPv4(t, b, checker.TCP(
  8325  		checker.DstPort(context.TestPort),
  8326  		checker.TCPSeqNum(next),
  8327  		checker.TCPAckNum(uint32(0)),
  8328  		checker.TCPFlags(header.TCPFlagRst),
  8329  	),
  8330  	)
  8331  
  8332  	ept := endpointTester{c.EP}
  8333  	ept.CheckReadError(t, &tcpip.ErrTimeout{})
  8334  
  8335  	if got, want := c.Stack().Stats().TCP.EstablishedTimedout.Value(), origEstablishedTimedout+1; got != want {
  8336  		t.Errorf("got c.Stack().Stats().TCP.EstablishedTimedout = %d, want = %d", got, want)
  8337  	}
  8338  	if got := c.Stack().Stats().TCP.CurrentConnected.Value(); got != 0 {
  8339  		t.Errorf("got stats.TCP.CurrentConnected.Value() = %d, want = 0", got)
  8340  	}
  8341  }
  8342  
  8343  func TestKeepaliveWithUserTimeout(t *testing.T) {
  8344  	c := context.New(t, e2e.DefaultMTU)
  8345  	defer c.Cleanup()
  8346  
  8347  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  8348  
  8349  	origEstablishedTimedout := c.Stack().Stats().TCP.EstablishedTimedout.Value()
  8350  
  8351  	const keepAliveIdle = 100 * time.Millisecond
  8352  	const keepAliveInterval = 3 * time.Second
  8353  	keepAliveIdleOption := tcpip.KeepaliveIdleOption(keepAliveIdle)
  8354  	if err := c.EP.SetSockOpt(&keepAliveIdleOption); err != nil {
  8355  		t.Fatalf("c.EP.SetSockOpt(&%T(%s)): %s", keepAliveIdleOption, keepAliveIdle, err)
  8356  	}
  8357  	keepAliveIntervalOption := tcpip.KeepaliveIntervalOption(keepAliveInterval)
  8358  	if err := c.EP.SetSockOpt(&keepAliveIntervalOption); err != nil {
  8359  		t.Fatalf("c.EP.SetSockOpt(&%T(%s)): %s", keepAliveIntervalOption, keepAliveInterval, err)
  8360  	}
  8361  	if err := c.EP.SetSockOptInt(tcpip.KeepaliveCountOption, 10); err != nil {
  8362  		t.Fatalf("c.EP.SetSockOptInt(tcpip.KeepaliveCountOption, 10): %s", err)
  8363  	}
  8364  	c.EP.SocketOptions().SetKeepAlive(true)
  8365  
  8366  	// Set userTimeout to be the duration to be 1 keepalive
  8367  	// probes. Which means that after the first probe is sent
  8368  	// the second one should cause the connection to be
  8369  	// closed due to userTimeout being hit.
  8370  	userTimeout := tcpip.TCPUserTimeoutOption(keepAliveInterval)
  8371  	if err := c.EP.SetSockOpt(&userTimeout); err != nil {
  8372  		t.Fatalf("c.EP.SetSockOpt(&%T(%s)): %s", userTimeout, keepAliveInterval, err)
  8373  	}
  8374  
  8375  	// Check that the connection is still alive.
  8376  	ept := endpointTester{c.EP}
  8377  	ept.CheckReadError(t, &tcpip.ErrWouldBlock{})
  8378  
  8379  	// Now receive 1 keepalives, but don't ACK it.
  8380  	b := c.GetPacket()
  8381  	defer b.Release()
  8382  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  8383  	checker.IPv4(t, b,
  8384  		checker.TCP(
  8385  			checker.DstPort(context.TestPort),
  8386  			checker.TCPSeqNum(uint32(c.IRS)),
  8387  			checker.TCPAckNum(uint32(iss)),
  8388  			checker.TCPFlags(header.TCPFlagAck),
  8389  		),
  8390  	)
  8391  
  8392  	// Sleep for a little over the KeepAlive interval to make sure
  8393  	// the timer has time to fire after the last ACK and close the
  8394  	// close the socket.
  8395  	time.Sleep(keepAliveInterval + keepAliveInterval/2)
  8396  
  8397  	// The connection should be closed with a timeout.
  8398  	// Send an ACK to trigger a RST from the stack as the endpoint should
  8399  	// be dead.
  8400  	c.SendPacket(nil, &context.Headers{
  8401  		SrcPort: context.TestPort,
  8402  		DstPort: c.Port,
  8403  		Flags:   header.TCPFlagAck,
  8404  		SeqNum:  iss,
  8405  		AckNum:  c.IRS + 1,
  8406  		RcvWnd:  30000,
  8407  	})
  8408  
  8409  	v := c.GetPacket()
  8410  	defer v.Release()
  8411  	checker.IPv4(t, v, checker.TCP(
  8412  		checker.DstPort(context.TestPort),
  8413  		checker.TCPSeqNum(uint32(c.IRS+1)),
  8414  		checker.TCPAckNum(uint32(0)),
  8415  		checker.TCPFlags(header.TCPFlagRst),
  8416  	),
  8417  	)
  8418  
  8419  	ept.CheckReadError(t, &tcpip.ErrTimeout{})
  8420  	if got, want := c.Stack().Stats().TCP.EstablishedTimedout.Value(), origEstablishedTimedout+1; got != want {
  8421  		t.Errorf("got c.Stack().Stats().TCP.EstablishedTimedout = %d, want = %d", got, want)
  8422  	}
  8423  	if got := c.Stack().Stats().TCP.CurrentConnected.Value(); got != 0 {
  8424  		t.Errorf("got stats.TCP.CurrentConnected.Value() = %d, want = 0", got)
  8425  	}
  8426  }
  8427  
  8428  func TestIncreaseWindowOnRead(t *testing.T) {
  8429  	// This test ensures that the endpoint sends an ack,
  8430  	// after read() when the window grows by more than 1 MSS.
  8431  	c := context.New(t, e2e.DefaultMTU)
  8432  	defer c.Cleanup()
  8433  
  8434  	const rcvBuf = 65535 * 10
  8435  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, rcvBuf)
  8436  
  8437  	// Write chunks of ~30000 bytes. It's important that two
  8438  	// payloads make it equal or longer than MSS.
  8439  	remain := rcvBuf * 2
  8440  	sent := 0
  8441  	data := make([]byte, e2e.DefaultMTU/2)
  8442  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  8443  	for remain > len(data) {
  8444  		c.SendPacket(data, &context.Headers{
  8445  			SrcPort: context.TestPort,
  8446  			DstPort: c.Port,
  8447  			Flags:   header.TCPFlagAck,
  8448  			SeqNum:  iss.Add(seqnum.Size(sent)),
  8449  			AckNum:  c.IRS.Add(1),
  8450  			RcvWnd:  30000,
  8451  		})
  8452  		sent += len(data)
  8453  		remain -= len(data)
  8454  		pkt := c.GetPacket()
  8455  		defer pkt.Release()
  8456  		checker.IPv4(t, pkt,
  8457  			checker.PayloadLen(header.TCPMinimumSize),
  8458  			checker.TCP(
  8459  				checker.DstPort(context.TestPort),
  8460  				checker.TCPSeqNum(uint32(c.IRS)+1),
  8461  				checker.TCPAckNum(uint32(iss)+uint32(sent)),
  8462  				checker.TCPFlags(header.TCPFlagAck),
  8463  			),
  8464  		)
  8465  		// Break once the window drops below e2e.DefaultMTU/2
  8466  		if wnd := header.TCP(header.IPv4(pkt.AsSlice()).Payload()).WindowSize(); wnd < e2e.DefaultMTU/2 {
  8467  			break
  8468  		}
  8469  	}
  8470  
  8471  	// We now have < 1 MSS in the buffer space. Read at least > 2 MSS
  8472  	// worth of data as receive buffer space
  8473  	w := tcpip.LimitedWriter{
  8474  		W: ioutil.Discard,
  8475  		// e2e.DefaultMTU is a good enough estimate for the MSS used for this
  8476  		// connection.
  8477  		N: e2e.DefaultMTU * 2,
  8478  	}
  8479  	for w.N != 0 {
  8480  		_, err := c.EP.Read(&w, tcpip.ReadOptions{})
  8481  		if err != nil {
  8482  			t.Fatalf("Read failed: %s", err)
  8483  		}
  8484  	}
  8485  
  8486  	// After reading > MSS worth of data, we surely crossed MSS. See the ack:
  8487  	v := c.GetPacket()
  8488  	defer v.Release()
  8489  	checker.IPv4(t, v,
  8490  		checker.PayloadLen(header.TCPMinimumSize),
  8491  		checker.TCP(
  8492  			checker.DstPort(context.TestPort),
  8493  			checker.TCPSeqNum(uint32(c.IRS)+1),
  8494  			checker.TCPAckNum(uint32(iss)+uint32(sent)),
  8495  			checker.TCPWindow(uint16(0xffff)),
  8496  			checker.TCPFlags(header.TCPFlagAck),
  8497  		),
  8498  	)
  8499  }
  8500  
  8501  func TestIncreaseWindowOnBufferResize(t *testing.T) {
  8502  	// This test ensures that the endpoint sends an ack,
  8503  	// after available recv buffer grows to more than 1 MSS.
  8504  	c := context.New(t, e2e.DefaultMTU)
  8505  	defer c.Cleanup()
  8506  
  8507  	const rcvBuf = 65535 * 10
  8508  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, rcvBuf)
  8509  
  8510  	// Write chunks of ~30000 bytes. It's important that two
  8511  	// payloads make it equal or longer than MSS.
  8512  	remain := rcvBuf
  8513  	sent := 0
  8514  	data := make([]byte, e2e.DefaultMTU/2)
  8515  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  8516  	for remain > len(data) {
  8517  		c.SendPacket(data, &context.Headers{
  8518  			SrcPort: context.TestPort,
  8519  			DstPort: c.Port,
  8520  			Flags:   header.TCPFlagAck,
  8521  			SeqNum:  iss.Add(seqnum.Size(sent)),
  8522  			AckNum:  c.IRS.Add(1),
  8523  			RcvWnd:  30000,
  8524  		})
  8525  		sent += len(data)
  8526  		remain -= len(data)
  8527  		v := c.GetPacket()
  8528  		defer v.Release()
  8529  		checker.IPv4(t, v,
  8530  			checker.PayloadLen(header.TCPMinimumSize),
  8531  			checker.TCP(
  8532  				checker.DstPort(context.TestPort),
  8533  				checker.TCPSeqNum(uint32(c.IRS)+1),
  8534  				checker.TCPAckNum(uint32(iss)+uint32(sent)),
  8535  				checker.TCPWindowLessThanEq(0xffff),
  8536  				checker.TCPFlags(header.TCPFlagAck),
  8537  			),
  8538  		)
  8539  	}
  8540  
  8541  	// Increasing the buffer from should generate an ACK,
  8542  	// since window grew from small value to larger equal MSS
  8543  	c.EP.SocketOptions().SetReceiveBufferSize(rcvBuf*4, true /* notify */)
  8544  	v := c.GetPacket()
  8545  	defer v.Release()
  8546  	checker.IPv4(t, v,
  8547  		checker.PayloadLen(header.TCPMinimumSize),
  8548  		checker.TCP(
  8549  			checker.DstPort(context.TestPort),
  8550  			checker.TCPSeqNum(uint32(c.IRS)+1),
  8551  			checker.TCPAckNum(uint32(iss)+uint32(sent)),
  8552  			checker.TCPWindow(uint16(0xffff)),
  8553  			checker.TCPFlags(header.TCPFlagAck),
  8554  		),
  8555  	)
  8556  }
  8557  
  8558  func TestTCPDeferAccept(t *testing.T) {
  8559  	c := context.New(t, e2e.DefaultMTU)
  8560  	defer c.Cleanup()
  8561  
  8562  	c.Create(-1)
  8563  
  8564  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  8565  		t.Fatal("Bind failed:", err)
  8566  	}
  8567  
  8568  	if err := c.EP.Listen(10); err != nil {
  8569  		t.Fatal("Listen failed:", err)
  8570  	}
  8571  
  8572  	const tcpDeferAccept = 1 * time.Second
  8573  	tcpDeferAcceptOption := tcpip.TCPDeferAcceptOption(tcpDeferAccept)
  8574  	if err := c.EP.SetSockOpt(&tcpDeferAcceptOption); err != nil {
  8575  		t.Fatalf("c.EP.SetSockOpt(&%T(%s)): %s", tcpDeferAcceptOption, tcpDeferAccept, err)
  8576  	}
  8577  
  8578  	irs, iss := executeHandshake(t, c, context.TestPort, false /* synCookiesInUse */)
  8579  
  8580  	_, _, err := c.EP.Accept(nil)
  8581  	if d := cmp.Diff(&tcpip.ErrWouldBlock{}, err); d != "" {
  8582  		t.Fatalf("c.EP.Accept(nil) mismatch (-want +got):\n%s", d)
  8583  	}
  8584  
  8585  	// Send data. This should result in an acceptable endpoint.
  8586  	c.SendPacket([]byte{1, 2, 3, 4}, &context.Headers{
  8587  		SrcPort: context.TestPort,
  8588  		DstPort: context.StackPort,
  8589  		Flags:   header.TCPFlagAck,
  8590  		SeqNum:  irs + 1,
  8591  		AckNum:  iss + 1,
  8592  	})
  8593  
  8594  	// Receive ACK for the data we sent.
  8595  	v := c.GetPacket()
  8596  	defer v.Release()
  8597  	checker.IPv4(t, v, checker.TCP(
  8598  		checker.DstPort(context.TestPort),
  8599  		checker.TCPFlags(header.TCPFlagAck),
  8600  		checker.TCPSeqNum(uint32(iss+1)),
  8601  		checker.TCPAckNum(uint32(irs+5))))
  8602  
  8603  	// Give a bit of time for the socket to be delivered to the accept queue.
  8604  	time.Sleep(50 * time.Millisecond)
  8605  	aep, _, err := c.EP.Accept(nil)
  8606  	if err != nil {
  8607  		t.Fatalf("got c.EP.Accept(nil) = %s, want: nil", err)
  8608  	}
  8609  
  8610  	aep.Close()
  8611  	// Closing aep without reading the data should trigger a RST.
  8612  	v = c.GetPacket()
  8613  	defer v.Release()
  8614  	checker.IPv4(t, v, checker.TCP(
  8615  		checker.DstPort(context.TestPort),
  8616  		checker.TCPFlags(header.TCPFlagRst|header.TCPFlagAck),
  8617  		checker.TCPSeqNum(uint32(iss+1)),
  8618  		checker.TCPAckNum(uint32(irs+5))))
  8619  }
  8620  
  8621  func TestTCPDeferAcceptTimeout(t *testing.T) {
  8622  	c := context.New(t, e2e.DefaultMTU)
  8623  	defer c.Cleanup()
  8624  
  8625  	c.Create(-1)
  8626  
  8627  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  8628  		t.Fatal("Bind failed:", err)
  8629  	}
  8630  
  8631  	if err := c.EP.Listen(10); err != nil {
  8632  		t.Fatal("Listen failed:", err)
  8633  	}
  8634  
  8635  	const tcpDeferAccept = 1 * time.Second
  8636  	tcpDeferAcceptOpt := tcpip.TCPDeferAcceptOption(tcpDeferAccept)
  8637  	if err := c.EP.SetSockOpt(&tcpDeferAcceptOpt); err != nil {
  8638  		t.Fatalf("c.EP.SetSockOpt(&%T(%s)) failed: %s", tcpDeferAcceptOpt, tcpDeferAccept, err)
  8639  	}
  8640  
  8641  	irs, iss := executeHandshake(t, c, context.TestPort, false /* synCookiesInUse */)
  8642  
  8643  	_, _, err := c.EP.Accept(nil)
  8644  	if d := cmp.Diff(&tcpip.ErrWouldBlock{}, err); d != "" {
  8645  		t.Fatalf("c.EP.Accept(nil) mismatch (-want +got):\n%s", d)
  8646  	}
  8647  
  8648  	// Sleep for a little of the tcpDeferAccept timeout.
  8649  	time.Sleep(tcpDeferAccept + 100*time.Millisecond)
  8650  
  8651  	// On timeout expiry we should get a SYN-ACK retransmission.
  8652  	v := c.GetPacket()
  8653  	defer v.Release()
  8654  	checker.IPv4(t, v, checker.TCP(
  8655  		checker.SrcPort(context.StackPort),
  8656  		checker.DstPort(context.TestPort),
  8657  		checker.TCPFlags(header.TCPFlagAck|header.TCPFlagSyn),
  8658  		checker.TCPAckNum(uint32(irs)+1)))
  8659  
  8660  	// Send data. This should result in an acceptable endpoint.
  8661  	c.SendPacket([]byte{1, 2, 3, 4}, &context.Headers{
  8662  		SrcPort: context.TestPort,
  8663  		DstPort: context.StackPort,
  8664  		Flags:   header.TCPFlagAck,
  8665  		SeqNum:  irs + 1,
  8666  		AckNum:  iss + 1,
  8667  	})
  8668  
  8669  	// Receive ACK for the data we sent.
  8670  	v = c.GetPacket()
  8671  	defer v.Release()
  8672  	checker.IPv4(t, v, checker.TCP(
  8673  		checker.SrcPort(context.StackPort),
  8674  		checker.DstPort(context.TestPort),
  8675  		checker.TCPFlags(header.TCPFlagAck),
  8676  		checker.TCPSeqNum(uint32(iss+1)),
  8677  		checker.TCPAckNum(uint32(irs+5))))
  8678  
  8679  	// Give sometime for the endpoint to be delivered to the accept queue.
  8680  	time.Sleep(50 * time.Millisecond)
  8681  	aep, _, err := c.EP.Accept(nil)
  8682  	if err != nil {
  8683  		t.Fatalf("got c.EP.Accept(nil) = %s, want: nil", err)
  8684  	}
  8685  
  8686  	aep.Close()
  8687  	// Closing aep without reading the data should trigger a RST.
  8688  	v = c.GetPacket()
  8689  	defer v.Release()
  8690  	checker.IPv4(t, v, checker.TCP(
  8691  		checker.SrcPort(context.StackPort),
  8692  		checker.DstPort(context.TestPort),
  8693  		checker.TCPFlags(header.TCPFlagRst|header.TCPFlagAck),
  8694  		checker.TCPSeqNum(uint32(iss+1)),
  8695  		checker.TCPAckNum(uint32(irs+5))))
  8696  }
  8697  
  8698  func TestResetDuringClose(t *testing.T) {
  8699  	c := context.New(t, e2e.DefaultMTU)
  8700  	defer c.Cleanup()
  8701  
  8702  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRecvBuf */)
  8703  	// Send some data to make sure there is some unread
  8704  	// data to trigger a reset on c.Close.
  8705  	irs := c.IRS
  8706  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  8707  	c.SendPacket([]byte{1, 2, 3, 4}, &context.Headers{
  8708  		SrcPort: context.TestPort,
  8709  		DstPort: c.Port,
  8710  		Flags:   header.TCPFlagAck,
  8711  		SeqNum:  iss,
  8712  		AckNum:  irs.Add(1),
  8713  		RcvWnd:  30000,
  8714  	})
  8715  
  8716  	// Receive ACK for the data we sent.
  8717  	v := c.GetPacket()
  8718  	defer v.Release()
  8719  	checker.IPv4(t, v, checker.TCP(
  8720  		checker.DstPort(context.TestPort),
  8721  		checker.TCPFlags(header.TCPFlagAck),
  8722  		checker.TCPSeqNum(uint32(irs.Add(1))),
  8723  		checker.TCPAckNum(uint32(iss)+4)))
  8724  
  8725  	// Close in a separate goroutine so that we can trigger
  8726  	// a race with the RST we send below. This should not
  8727  	// panic due to the route being released depending on
  8728  	// whether Close() sends an active RST or the RST sent
  8729  	// below is processed by the worker first.
  8730  	var wg sync.WaitGroup
  8731  
  8732  	wg.Add(1)
  8733  	go func() {
  8734  		defer wg.Done()
  8735  		c.SendPacket(nil, &context.Headers{
  8736  			SrcPort: context.TestPort,
  8737  			DstPort: c.Port,
  8738  			SeqNum:  iss.Add(4),
  8739  			AckNum:  c.IRS.Add(5),
  8740  			RcvWnd:  30000,
  8741  			Flags:   header.TCPFlagRst,
  8742  		})
  8743  	}()
  8744  
  8745  	wg.Add(1)
  8746  	go func() {
  8747  		defer wg.Done()
  8748  		c.EP.Close()
  8749  	}()
  8750  
  8751  	wg.Wait()
  8752  }
  8753  
  8754  func TestStackTimeWaitReuse(t *testing.T) {
  8755  	c := context.New(t, e2e.DefaultMTU)
  8756  	defer c.Cleanup()
  8757  
  8758  	s := c.Stack()
  8759  	var twReuse tcpip.TCPTimeWaitReuseOption
  8760  	if err := s.TransportProtocolOption(tcp.ProtocolNumber, &twReuse); err != nil {
  8761  		t.Fatalf("s.TransportProtocolOption(%v, %v) = %v", tcp.ProtocolNumber, &twReuse, err)
  8762  	}
  8763  	if got, want := twReuse, tcpip.TCPTimeWaitReuseLoopbackOnly; got != want {
  8764  		t.Fatalf("got tcpip.TCPTimeWaitReuseOption: %v, want: %v", got, want)
  8765  	}
  8766  }
  8767  
  8768  func TestSetStackTimeWaitReuse(t *testing.T) {
  8769  	c := context.New(t, e2e.DefaultMTU)
  8770  	defer c.Cleanup()
  8771  
  8772  	s := c.Stack()
  8773  	testCases := []struct {
  8774  		v   int
  8775  		err tcpip.Error
  8776  	}{
  8777  		{int(tcpip.TCPTimeWaitReuseDisabled), nil},
  8778  		{int(tcpip.TCPTimeWaitReuseGlobal), nil},
  8779  		{int(tcpip.TCPTimeWaitReuseLoopbackOnly), nil},
  8780  		{int(tcpip.TCPTimeWaitReuseLoopbackOnly) + 1, &tcpip.ErrInvalidOptionValue{}},
  8781  		{int(tcpip.TCPTimeWaitReuseDisabled) - 1, &tcpip.ErrInvalidOptionValue{}},
  8782  	}
  8783  
  8784  	for _, tc := range testCases {
  8785  		opt := tcpip.TCPTimeWaitReuseOption(tc.v)
  8786  		err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &opt)
  8787  		if got, want := err, tc.err; got != want {
  8788  			t.Fatalf("s.SetTransportProtocolOption(%d, &%T(%d)) = %s, want = %s", tcp.ProtocolNumber, tc.v, tc.v, err, tc.err)
  8789  		}
  8790  		if tc.err != nil {
  8791  			continue
  8792  		}
  8793  
  8794  		var twReuse tcpip.TCPTimeWaitReuseOption
  8795  		if err := s.TransportProtocolOption(tcp.ProtocolNumber, &twReuse); err != nil {
  8796  			t.Fatalf("s.TransportProtocolOption(%v, %v) = %v, want nil", tcp.ProtocolNumber, &twReuse, err)
  8797  		}
  8798  
  8799  		if got, want := twReuse, tcpip.TCPTimeWaitReuseOption(tc.v); got != want {
  8800  			t.Fatalf("got tcpip.TCPTimeWaitReuseOption: %v, want: %v", got, want)
  8801  		}
  8802  	}
  8803  }
  8804  
  8805  func TestHandshakeRTT(t *testing.T) {
  8806  	type testCase struct {
  8807  		connect   bool
  8808  		tsEnabled bool
  8809  		useCookie bool
  8810  		retrans   bool
  8811  		delay     time.Duration
  8812  		wantRTT   time.Duration
  8813  	}
  8814  	var testCases []testCase
  8815  	for _, connect := range []bool{false, true} {
  8816  		for _, tsEnabled := range []bool{false, true} {
  8817  			for _, useCookie := range []bool{false, true} {
  8818  				for _, retrans := range []bool{false, true} {
  8819  					if connect && useCookie {
  8820  						continue
  8821  					}
  8822  					delay := 800 * time.Millisecond
  8823  					if retrans {
  8824  						delay = 1200 * time.Millisecond
  8825  					}
  8826  					wantRTT := delay
  8827  					// If syncookie is enabled, sample RTT only when TS option is enabled.
  8828  					if !retrans && useCookie && !tsEnabled {
  8829  						wantRTT = 0
  8830  					}
  8831  					// If retransmitted, sample RTT only when TS option is enabled.
  8832  					if retrans && !tsEnabled {
  8833  						wantRTT = 0
  8834  					}
  8835  					testCases = append(testCases, testCase{connect, tsEnabled, useCookie, retrans, delay, wantRTT})
  8836  				}
  8837  			}
  8838  		}
  8839  	}
  8840  	for _, tt := range testCases {
  8841  		tt := tt
  8842  		t.Run(fmt.Sprintf("connect=%t,TS=%t,cookie=%t,retrans=%t)", tt.connect, tt.tsEnabled, tt.useCookie, tt.retrans), func(t *testing.T) {
  8843  			t.Parallel()
  8844  			c := context.New(t, e2e.DefaultMTU)
  8845  			defer c.Cleanup()
  8846  			if tt.useCookie {
  8847  				opt := tcpip.TCPAlwaysUseSynCookies(true)
  8848  				if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  8849  					t.Fatalf("SetTransportProtocolOption(%d, &%T(%t)): %s", tcp.ProtocolNumber, opt, opt, err)
  8850  				}
  8851  			}
  8852  			synOpts := header.TCPSynOptions{}
  8853  			if tt.tsEnabled {
  8854  				synOpts.TS = true
  8855  				synOpts.TSVal = 42
  8856  			}
  8857  			if tt.connect {
  8858  				c.CreateConnectedWithOptions(synOpts, tt.delay)
  8859  			} else {
  8860  				synOpts.MSS = e2e.DefaultIPv4MSS
  8861  				synOpts.WS = -1
  8862  				c.AcceptWithOptions(-1, synOpts, tt.delay)
  8863  			}
  8864  			var info tcpip.TCPInfoOption
  8865  			if err := c.EP.GetSockOpt(&info); err != nil {
  8866  				t.Fatalf("c.EP.GetSockOpt(&%T) = %s", info, err)
  8867  			}
  8868  			if got := info.RTT.Round(tt.wantRTT); got != tt.wantRTT {
  8869  				t.Fatalf("got info.RTT=%s, expect %s", got, tt.wantRTT)
  8870  			}
  8871  			if info.RTTVar != 0 && tt.wantRTT == 0 {
  8872  				t.Fatalf("got info.RTTVar=%s, expect 0", info.RTTVar)
  8873  			}
  8874  			if info.RTTVar == 0 && tt.wantRTT != 0 {
  8875  				t.Fatalf("got info.RTTVar=0, expect non zero")
  8876  			}
  8877  		})
  8878  	}
  8879  }
  8880  
  8881  func TestSetRTO(t *testing.T) {
  8882  	c := context.New(t, e2e.DefaultMTU)
  8883  	minRTO, maxRTO := tcpRTOMinMax(t, c)
  8884  	c.Cleanup()
  8885  	for _, tt := range []struct {
  8886  		name   string
  8887  		RTO    time.Duration
  8888  		minRTO time.Duration
  8889  		maxRTO time.Duration
  8890  		err    tcpip.Error
  8891  	}{
  8892  		{
  8893  			name:   "invalid minRTO",
  8894  			minRTO: maxRTO + time.Second,
  8895  			err:    &tcpip.ErrInvalidOptionValue{},
  8896  		},
  8897  		{
  8898  			name:   "invalid maxRTO",
  8899  			maxRTO: minRTO - time.Millisecond,
  8900  			err:    &tcpip.ErrInvalidOptionValue{},
  8901  		},
  8902  		{
  8903  			name:   "valid minRTO",
  8904  			minRTO: maxRTO - time.Second,
  8905  		},
  8906  		{
  8907  			name:   "valid maxRTO",
  8908  			maxRTO: minRTO + time.Millisecond,
  8909  		},
  8910  	} {
  8911  		t.Run(tt.name, func(t *testing.T) {
  8912  			c := context.New(t, e2e.DefaultMTU)
  8913  			defer c.Cleanup()
  8914  			var opt tcpip.SettableTransportProtocolOption
  8915  			if tt.minRTO > 0 {
  8916  				min := tcpip.TCPMinRTOOption(tt.minRTO)
  8917  				opt = &min
  8918  			}
  8919  			if tt.maxRTO > 0 {
  8920  				max := tcpip.TCPMaxRTOOption(tt.maxRTO)
  8921  				opt = &max
  8922  			}
  8923  			err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, opt)
  8924  			if got, want := err, tt.err; got != want {
  8925  				t.Fatalf("c.Stack().SetTransportProtocolOption(TCP, &%T(%v)) = %v, want = %v", opt, opt, got, want)
  8926  			}
  8927  			if tt.err == nil {
  8928  				minRTO, maxRTO := tcpRTOMinMax(t, c)
  8929  				if tt.minRTO > 0 && tt.minRTO != minRTO {
  8930  					t.Fatalf("got minRTO = %s, want %s", minRTO, tt.minRTO)
  8931  				}
  8932  				if tt.maxRTO > 0 && tt.maxRTO != maxRTO {
  8933  					t.Fatalf("got maxRTO = %s, want %s", maxRTO, tt.maxRTO)
  8934  				}
  8935  			}
  8936  		})
  8937  	}
  8938  }
  8939  
  8940  func tcpRTOMinMax(t *testing.T, c *context.Context) (time.Duration, time.Duration) {
  8941  	t.Helper()
  8942  	var minOpt tcpip.TCPMinRTOOption
  8943  	var maxOpt tcpip.TCPMaxRTOOption
  8944  	if err := c.Stack().TransportProtocolOption(tcp.ProtocolNumber, &minOpt); err != nil {
  8945  		t.Fatalf("c.Stack().TransportProtocolOption(TCP, %T): %s", minOpt, err)
  8946  	}
  8947  	if err := c.Stack().TransportProtocolOption(tcp.ProtocolNumber, &maxOpt); err != nil {
  8948  		t.Fatalf("c.Stack().TransportProtocolOption(TCP, %T): %s", maxOpt, err)
  8949  	}
  8950  	return time.Duration(minOpt), time.Duration(maxOpt)
  8951  }
  8952  
  8953  // generateRandomPayload generates a random byte slice of the specified length
  8954  // causing a fatal test failure if it is unable to do so.
  8955  func generateRandomPayload(t *testing.T, n int) []byte {
  8956  	t.Helper()
  8957  	buf := make([]byte, n)
  8958  	if _, err := rand.Read(buf); err != nil {
  8959  		t.Fatalf("rand.Read(buf) failed: %s", err)
  8960  	}
  8961  	return buf
  8962  }
  8963  
  8964  func TestSendBufferTuning(t *testing.T) {
  8965  	const maxPayload = 536
  8966  	const mtu = header.TCPMinimumSize + header.IPv4MinimumSize + e2e.MaxTCPOptionSize + maxPayload
  8967  	const packetOverheadFactor = 2
  8968  
  8969  	testCases := []struct {
  8970  		name               string
  8971  		autoTuningDisabled bool
  8972  	}{
  8973  		{"autoTuningDisabled", true},
  8974  		{"autoTuningEnabled", false},
  8975  	}
  8976  
  8977  	for _, tc := range testCases {
  8978  		t.Run(tc.name, func(t *testing.T) {
  8979  			c := context.New(t, mtu)
  8980  			defer c.Cleanup()
  8981  
  8982  			// Set the stack option for send buffer size.
  8983  			const defaultSndBufSz = maxPayload * tcp.InitialCwnd
  8984  			const maxSndBufSz = defaultSndBufSz * 10
  8985  			{
  8986  				opt := tcpip.TCPSendBufferSizeRangeOption{Min: 1, Default: defaultSndBufSz, Max: maxSndBufSz}
  8987  				if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  8988  					t.Fatalf("SetTransportProtocolOption(%d, &%#v): %s", tcp.ProtocolNumber, opt, err)
  8989  				}
  8990  			}
  8991  
  8992  			c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  8993  
  8994  			oldSz := c.EP.SocketOptions().GetSendBufferSize()
  8995  			if oldSz != defaultSndBufSz {
  8996  				t.Fatalf("Wrong send buffer size got %d want %d", oldSz, defaultSndBufSz)
  8997  			}
  8998  
  8999  			if tc.autoTuningDisabled {
  9000  				c.EP.SocketOptions().SetSendBufferSize(defaultSndBufSz, true /* notify */)
  9001  			}
  9002  
  9003  			data := make([]byte, maxPayload)
  9004  			for i := range data {
  9005  				data[i] = byte(i)
  9006  			}
  9007  
  9008  			w, ch := waiter.NewChannelEntry(waiter.WritableEvents)
  9009  			c.WQ.EventRegister(&w)
  9010  			defer c.WQ.EventUnregister(&w)
  9011  
  9012  			bytesRead := 0
  9013  			for {
  9014  				// Packets will be sent till the send buffer
  9015  				// size is reached.
  9016  				var r bytes.Reader
  9017  				r.Reset(data[bytesRead : bytesRead+maxPayload])
  9018  				_, err := c.EP.Write(&r, tcpip.WriteOptions{})
  9019  				if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  9020  					break
  9021  				}
  9022  
  9023  				c.ReceiveAndCheckPacketWithOptions(data, bytesRead, maxPayload, 0)
  9024  				bytesRead += maxPayload
  9025  				data = append(data, data...)
  9026  			}
  9027  
  9028  			// Send an ACK and wait for connection to become writable again.
  9029  			c.SendAck(seqnum.Value(context.TestInitialSequenceNumber).Add(1), bytesRead)
  9030  			select {
  9031  			case <-ch:
  9032  				if err := c.EP.LastError(); err != nil {
  9033  					t.Fatalf("Write failed: %s", err)
  9034  				}
  9035  			case <-time.After(1 * time.Second):
  9036  				t.Fatalf("Timed out waiting for connection")
  9037  			}
  9038  
  9039  			outSz := int64(defaultSndBufSz)
  9040  			if !tc.autoTuningDisabled {
  9041  				// Calculate the new auto tuned send buffer.
  9042  				var info tcpip.TCPInfoOption
  9043  				if err := c.EP.GetSockOpt(&info); err != nil {
  9044  					t.Fatalf("GetSockOpt failed: %v", err)
  9045  				}
  9046  				outSz = int64(info.SndCwnd) * packetOverheadFactor * maxPayload
  9047  			}
  9048  
  9049  			if newSz := c.EP.SocketOptions().GetSendBufferSize(); newSz != outSz {
  9050  				t.Fatalf("Wrong send buffer size, got %d want %d", newSz, outSz)
  9051  			}
  9052  		})
  9053  	}
  9054  }
  9055  
  9056  func TestTimestampSynCookies(t *testing.T) {
  9057  	clock := faketime.NewManualClock()
  9058  	tsNow := func() uint32 {
  9059  		return uint32(clock.NowMonotonic().Sub(tcpip.MonotonicTime{}).Milliseconds())
  9060  	}
  9061  	// Advance the clock so that NowMonotonic is non-zero.
  9062  	clock.Advance(time.Second)
  9063  	c := context.NewWithOpts(t, context.Options{
  9064  		EnableV4: true,
  9065  		EnableV6: true,
  9066  		MTU:      e2e.DefaultMTU,
  9067  		Clock:    clock,
  9068  	})
  9069  	defer c.Cleanup()
  9070  	opt := tcpip.TCPAlwaysUseSynCookies(true)
  9071  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, &opt); err != nil {
  9072  		t.Fatalf("SetTransportProtocolOption(%d, &%T(%t)): %s", tcp.ProtocolNumber, opt, opt, err)
  9073  	}
  9074  	wq := &waiter.Queue{}
  9075  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  9076  	if err != nil {
  9077  		t.Fatalf("NewEndpoint failed: %s", err)
  9078  	}
  9079  	defer ep.Close()
  9080  
  9081  	tcpOpts := [12]byte{header.TCPOptionNOP, header.TCPOptionNOP}
  9082  	header.EncodeTSOption(42, 0, tcpOpts[2:])
  9083  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  9084  		t.Fatalf("Bind failed: %s", err)
  9085  	}
  9086  	if err := ep.Listen(10); err != nil {
  9087  		t.Fatalf("Listen failed: %s", err)
  9088  	}
  9089  	iss := seqnum.Value(context.TestInitialSequenceNumber)
  9090  	c.SendPacket(nil, &context.Headers{
  9091  		SrcPort: context.TestPort,
  9092  		DstPort: context.StackPort,
  9093  		Flags:   header.TCPFlagSyn,
  9094  		RcvWnd:  seqnum.Size(512),
  9095  		SeqNum:  iss,
  9096  		TCPOpts: tcpOpts[:],
  9097  	})
  9098  	// Get the TSVal of SYN-ACK.
  9099  	b := c.GetPacket()
  9100  	defer b.Release()
  9101  	tcpHdr := header.TCP(header.IPv4(b.AsSlice()).Payload())
  9102  	c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
  9103  	initialTSVal := tcpHdr.ParsedOptions().TSVal
  9104  	// derive the tsOffset.
  9105  	tsOffset := initialTSVal - tsNow()
  9106  
  9107  	header.EncodeTSOption(420, initialTSVal, tcpOpts[2:])
  9108  	c.SendPacket(nil, &context.Headers{
  9109  		SrcPort: context.TestPort,
  9110  		DstPort: context.StackPort,
  9111  		Flags:   header.TCPFlagAck,
  9112  		RcvWnd:  seqnum.Size(512),
  9113  		SeqNum:  iss + 1,
  9114  		AckNum:  c.IRS + 1,
  9115  		TCPOpts: tcpOpts[:],
  9116  	})
  9117  	c.EP, _, err = ep.Accept(nil)
  9118  	// Try to accept the connection.
  9119  	we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  9120  	wq.EventRegister(&we)
  9121  	defer wq.EventUnregister(&we)
  9122  	if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  9123  		// Wait for connection to be established.
  9124  		select {
  9125  		case <-ch:
  9126  			c.EP, _, err = ep.Accept(nil)
  9127  			if err != nil {
  9128  				t.Fatalf("Accept failed: %s", err)
  9129  			}
  9130  
  9131  		case <-time.After(1 * time.Second):
  9132  			t.Fatalf("Timed out waiting for accept")
  9133  		}
  9134  	} else if err != nil {
  9135  		t.Fatalf("failed to accept: %s", err)
  9136  	}
  9137  
  9138  	// Advance the clock again so that we expect the next TSVal to change.
  9139  	clock.Advance(time.Second)
  9140  	data := []byte{1, 2, 3}
  9141  	var r bytes.Reader
  9142  	r.Reset(data)
  9143  	if _, err := c.EP.Write(&r, tcpip.WriteOptions{}); err != nil {
  9144  		t.Fatalf("Write failed: %s", err)
  9145  	}
  9146  
  9147  	// The endpoint should have a correct TSOffset so that the received TSVal
  9148  	// should match our expectation.
  9149  	p := c.GetPacket()
  9150  	defer p.Release()
  9151  	if got, want := header.TCP(header.IPv4(p.AsSlice()).Payload()).ParsedOptions().TSVal, tsNow()+tsOffset; got != want {
  9152  		t.Fatalf("got TSVal = %d, want %d", got, want)
  9153  	}
  9154  }
  9155  
  9156  // TestECNFlagsAccept tests that an ECN non-setup/setup SYN is accepted
  9157  // and the connection is correctly completed.
  9158  func TestECNFlagsAccept(t *testing.T) {
  9159  	testCases := []struct {
  9160  		name  string
  9161  		flags header.TCPFlags
  9162  	}{
  9163  		{name: "non-setup ECN SYN w/ ECE", flags: header.TCPFlagEce},
  9164  		{name: "non-setup ECN SYN w/ CWR", flags: header.TCPFlagCwr},
  9165  		{name: "setup ECN SYN", flags: header.TCPFlagEce | header.TCPFlagCwr},
  9166  	}
  9167  
  9168  	for _, tc := range testCases {
  9169  		t.Run(tc.name, func(t *testing.T) {
  9170  
  9171  			c := context.New(t, e2e.DefaultMTU)
  9172  			defer c.Cleanup()
  9173  
  9174  			// Create EP and start listening.
  9175  			wq := &waiter.Queue{}
  9176  			ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  9177  			if err != nil {
  9178  				t.Fatalf("NewEndpoint failed: %s", err)
  9179  			}
  9180  			defer ep.Close()
  9181  
  9182  			if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  9183  				t.Fatalf("Bind failed: %s", err)
  9184  			}
  9185  
  9186  			if err := ep.Listen(10); err != nil {
  9187  				t.Fatalf("Listen failed: %s", err)
  9188  			}
  9189  
  9190  			// Do 3-way handshake.
  9191  			const maxPayload = 100
  9192  
  9193  			c.PassiveConnect(maxPayload, -1 /* wndScale */, header.TCPSynOptions{MSS: e2e.DefaultIPv4MSS, Flags: tc.flags})
  9194  
  9195  			// Try to accept the connection.
  9196  			we, ch := waiter.NewChannelEntry(waiter.ReadableEvents)
  9197  			wq.EventRegister(&we)
  9198  			defer wq.EventUnregister(&we)
  9199  
  9200  			c.EP, _, err = ep.Accept(nil)
  9201  			if cmp.Equal(&tcpip.ErrWouldBlock{}, err) {
  9202  				// Wait for connection to be established.
  9203  				select {
  9204  				case <-ch:
  9205  					c.EP, _, err = ep.Accept(nil)
  9206  					if err != nil {
  9207  						t.Fatalf("Accept failed: %s", err)
  9208  					}
  9209  
  9210  				case <-time.After(1 * time.Second):
  9211  					t.Fatalf("Timed out waiting for accept")
  9212  				}
  9213  			} else if err != nil {
  9214  				t.Fatalf("Accept failed: %s", err)
  9215  			}
  9216  		})
  9217  	}
  9218  }
  9219  
  9220  func TestReadAfterCloseWithBufferedData(t *testing.T) {
  9221  	c := context.New(t, e2e.DefaultMTU)
  9222  	defer c.Cleanup()
  9223  	con := c.CreateConnectedWithOptionsNoDelay(header.TCPSynOptions{})
  9224  	// Fill up the receive queue.
  9225  	for i := 0; i < 300; i++ {
  9226  		con.SendPacket([]byte{1, 2, 3, 4}, nil)
  9227  	}
  9228  
  9229  	timeout := time.After(5 * time.Second)
  9230  	// If the receive queue is not properly drained, the endpoint will never
  9231  	// return ErrClosedForReceive.
  9232  	c.EP.Close()
  9233  	for {
  9234  		select {
  9235  		case <-timeout:
  9236  			t.Fatalf("timed out waiting for read to return error %q", &tcpip.ErrClosedForReceive{})
  9237  			return
  9238  		default:
  9239  			if _, err := c.EP.Read(ioutil.Discard, tcpip.ReadOptions{}); cmp.Equal(err, &tcpip.ErrClosedForReceive{}) {
  9240  				return
  9241  			}
  9242  		}
  9243  	}
  9244  }
  9245  
  9246  func TestReleaseDanglingEndpoints(t *testing.T) {
  9247  	c := context.New(t, e2e.DefaultMTU)
  9248  	defer c.Cleanup()
  9249  
  9250  	c.CreateConnected(context.TestInitialSequenceNumber, 30000, -1 /* epRcvBuf */)
  9251  	ep := c.EP
  9252  	c.EP = nil
  9253  
  9254  	// Close the endpoint, make sure we get a FIN segment. The endpoint should be
  9255  	// dangling.
  9256  	ep.Close()
  9257  	iss := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
  9258  	v := c.GetPacket()
  9259  	defer v.Release()
  9260  	checker.IPv4(t, v, checker.TCP(
  9261  		checker.DstPort(context.TestPort),
  9262  		checker.TCPSeqNum(uint32(c.IRS)+1),
  9263  		checker.TCPAckNum(uint32(iss)),
  9264  		checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  9265  	))
  9266  	tcpip.ReleaseDanglingEndpoints()
  9267  
  9268  	// ReleaseDanglingEndpoints should abort the half-closed endpoint causing
  9269  	// a RST to be sent.
  9270  	v = c.GetPacket()
  9271  	defer v.Release()
  9272  	checker.IPv4(t, v, checker.TCP(
  9273  		checker.DstPort(context.TestPort),
  9274  		checker.TCPSeqNum(uint32(c.IRS)+2),
  9275  		checker.TCPAckNum(uint32(iss)),
  9276  		checker.TCPFlags(header.TCPFlagRst|header.TCPFlagAck),
  9277  	))
  9278  
  9279  	// Now send an ACK and it should trigger a RST as the endpoint is aborted.
  9280  	c.SendPacket(nil, &context.Headers{
  9281  		SrcPort: context.TestPort,
  9282  		DstPort: c.Port,
  9283  		Flags:   header.TCPFlagAck,
  9284  		SeqNum:  iss,
  9285  		AckNum:  c.IRS.Add(2),
  9286  		RcvWnd:  30000,
  9287  	})
  9288  
  9289  	v = c.GetPacket()
  9290  	defer v.Release()
  9291  	checker.IPv4(t, v, checker.TCP(
  9292  		checker.DstPort(context.TestPort),
  9293  		checker.TCPSeqNum(uint32(c.IRS)+2),
  9294  		checker.TCPAckNum(0),
  9295  		checker.TCPFlags(header.TCPFlagRst),
  9296  	))
  9297  }
  9298  
  9299  // TestLateSynCookieAck ensures that we properly handle the following case
  9300  // rather than sending a RST on a valid connection:
  9301  //
  9302  //   - We receive a SYN while under load and issue a SYN/ACK with cookie S.
  9303  //   - We receive a retransmitted SYN while space exists in the SYN queue, and
  9304  //     issue a SYN/ACK with seqnum S'.
  9305  //   - We receive an ACK based on S.
  9306  //   - We respond with an RST because we expected an ACK based on S'.
  9307  func TestLateSynCookieAck(t *testing.T) {
  9308  	c := context.New(t, e2e.DefaultMTU)
  9309  	defer c.Cleanup()
  9310  	stats := c.Stack().Stats()
  9311  	wq := &waiter.Queue{}
  9312  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  9313  	if err != nil {
  9314  		t.Fatalf("NewEndpoint failed: %s", err)
  9315  	}
  9316  
  9317  	initial := stats.TCP.CurrentEstablished.Value()
  9318  
  9319  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  9320  		t.Fatalf("Bind failed: %s", err)
  9321  	}
  9322  
  9323  	// With a backlog of 2, we get one slot in the SYN queue before we
  9324  	// start using SYN cookies. See
  9325  	// //pkg/tcpip/transport/tcp/accept.go:handleListenSegment:useSynCookies
  9326  	// for an explanation.
  9327  	if err := ep.Listen(2); err != nil {
  9328  		t.Fatalf("Listen failed: %s", err)
  9329  	}
  9330  
  9331  	// To reach our desired state, we're gonna do the following:
  9332  	//
  9333  	//   - Send SYN S1 to force subsequent SYNs to return cookies.
  9334  	//   - Send SYN S2, which returns a cookie SYN/ACK.
  9335  	//   - Finish S1's handshake, opening space in the SYN queue.
  9336  	//   - Retransmit S2, which will give use a different seqnum.
  9337  	//   - Finish S2's handshake with the cookie SYN/ACK.
  9338  
  9339  	// Send S1.
  9340  	const otherTestPort = context.TestPort + 1
  9341  	iss := seqnum.Value(context.TestInitialSequenceNumber)
  9342  	c.SendPacket(nil, &context.Headers{
  9343  		SrcPort: otherTestPort,
  9344  		DstPort: context.StackPort,
  9345  		Flags:   header.TCPFlagSyn,
  9346  		SeqNum:  iss,
  9347  	})
  9348  	// Receive the SYN-ACK reply.
  9349  	s1Reply := c.GetPacket()
  9350  	defer s1Reply.Release()
  9351  	s1ReplyHdr := header.TCP(header.IPv4(s1Reply.AsSlice()).Payload())
  9352  
  9353  	// Send S2.
  9354  	c.SendPacket(nil, &context.Headers{
  9355  		SrcPort: context.TestPort,
  9356  		DstPort: context.StackPort,
  9357  		Flags:   header.TCPFlagSyn,
  9358  		SeqNum:  iss,
  9359  	})
  9360  	// Receive the SYN-ACK reply.
  9361  	s2CookieReply := c.GetPacket()
  9362  	defer s2CookieReply.Release()
  9363  	s2CookieReplyHdr := header.TCP(header.IPv4(s2CookieReply.AsSlice()).Payload())
  9364  
  9365  	// Finish the S1 handshake.
  9366  	ackHeaders := &context.Headers{
  9367  		SrcPort: otherTestPort,
  9368  		DstPort: context.StackPort,
  9369  		Flags:   header.TCPFlagAck,
  9370  		SeqNum:  iss + 1,
  9371  		AckNum:  seqnum.Value(s1ReplyHdr.SequenceNumber()) + 1,
  9372  	}
  9373  	c.SendPacket(nil, ackHeaders)
  9374  
  9375  	// Wait for S1's connection to move from the SYN to the accept queue.
  9376  	metricPollFn := func() error {
  9377  		if got, want := stats.TCP.CurrentEstablished.Value(), initial+1; got != want {
  9378  			return fmt.Errorf("connection never established: got stats.TCP.CurrentEstablished.Value() = %d, want = %d", got, want)
  9379  		}
  9380  		return nil
  9381  	}
  9382  	if err := testutil.Poll(metricPollFn, 1*time.Second); err != nil {
  9383  		t.Fatal(err)
  9384  	}
  9385  
  9386  	// Retransmit S2.
  9387  	c.SendPacket(nil, &context.Headers{
  9388  		SrcPort: context.TestPort,
  9389  		DstPort: context.StackPort,
  9390  		Flags:   header.TCPFlagSyn,
  9391  		SeqNum:  iss,
  9392  	})
  9393  	// Receive the SYN-ACK reply.
  9394  	s2QueueReply := c.GetPacket()
  9395  	defer s2QueueReply.Release()
  9396  	s2QueueReplyHdr := header.TCP(header.IPv4(s2QueueReply.AsSlice()).Payload())
  9397  	if s2CookieReplyHdr.SequenceNumber() == s2QueueReplyHdr.SequenceNumber() {
  9398  		t.Fatalf("the SYN cookie and regular seqnum are equal; is the backlog too large?")
  9399  	}
  9400  
  9401  	// Finish S2's handshake using the cookie.
  9402  	ackHeaders = &context.Headers{
  9403  		SrcPort: context.TestPort,
  9404  		DstPort: context.StackPort,
  9405  		Flags:   header.TCPFlagAck,
  9406  		SeqNum:  iss + 1,
  9407  		AckNum:  seqnum.Value(s2CookieReplyHdr.SequenceNumber()) + 1,
  9408  	}
  9409  	c.SendPacket(nil, ackHeaders)
  9410  
  9411  	// Verify that we've completed two connections.
  9412  	metricPollFn = func() error {
  9413  		if got, want := stats.TCP.CurrentEstablished.Value(), initial+2; got != want {
  9414  			return fmt.Errorf("got stats.TCP.CurrentEstablished.Value() = %d, want = %d", got, want)
  9415  		}
  9416  		return nil
  9417  	}
  9418  	if err := testutil.Poll(metricPollFn, 1*time.Second); err != nil {
  9419  		t.Error(err)
  9420  	}
  9421  }
  9422  
  9423  func TestMain(m *testing.M) {
  9424  	refs.SetLeakMode(refs.LeaksPanic)
  9425  	code := m.Run()
  9426  	tcpip.ReleaseDanglingEndpoints()
  9427  	// Allow TCP async work to complete to avoid false reports of leaks.
  9428  	// TODO(gvisor.dev/issue/5940): Use fake clock in tests.
  9429  	time.Sleep(1 * time.Second)
  9430  	refs.DoLeakCheck()
  9431  	os.Exit(code)
  9432  }