github.com/lightlus/netstack@v1.2.0/tcpip/transport/tcp/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 "math" 21 "testing" 22 "time" 23 24 "github.com/lightlus/netstack/tcpip" 25 "github.com/lightlus/netstack/tcpip/buffer" 26 "github.com/lightlus/netstack/tcpip/checker" 27 "github.com/lightlus/netstack/tcpip/header" 28 "github.com/lightlus/netstack/tcpip/link/loopback" 29 "github.com/lightlus/netstack/tcpip/link/sniffer" 30 "github.com/lightlus/netstack/tcpip/network/ipv4" 31 "github.com/lightlus/netstack/tcpip/network/ipv6" 32 "github.com/lightlus/netstack/tcpip/ports" 33 "github.com/lightlus/netstack/tcpip/seqnum" 34 "github.com/lightlus/netstack/tcpip/stack" 35 "github.com/lightlus/netstack/tcpip/transport/tcp" 36 "github.com/lightlus/netstack/tcpip/transport/tcp/testing/context" 37 "github.com/lightlus/netstack/waiter" 38 ) 39 40 const ( 41 // defaultMTU is the MTU, in bytes, used throughout the tests, except 42 // where another value is explicitly used. It is chosen to match the MTU 43 // of loopback interfaces on linux systems. 44 defaultMTU = 65535 45 46 // defaultIPv4MSS is the MSS sent by the network stack in SYN/SYN-ACK for an 47 // IPv4 endpoint when the MTU is set to defaultMTU in the test. 48 defaultIPv4MSS = defaultMTU - header.IPv4MinimumSize - header.TCPMinimumSize 49 ) 50 51 func TestGiveUpConnect(t *testing.T) { 52 c := context.New(t, defaultMTU) 53 defer c.Cleanup() 54 55 var wq waiter.Queue 56 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq) 57 if err != nil { 58 t.Fatalf("NewEndpoint failed: %v", err) 59 } 60 61 // Register for notification, then start connection attempt. 62 waitEntry, notifyCh := waiter.NewChannelEntry(nil) 63 wq.EventRegister(&waitEntry, waiter.EventOut) 64 defer wq.EventUnregister(&waitEntry) 65 66 if err := ep.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrConnectStarted { 67 t.Fatalf("got ep.Connect(...) = %v, want = %v", err, tcpip.ErrConnectStarted) 68 } 69 70 // Close the connection, wait for completion. 71 ep.Close() 72 73 // Wait for ep to become writable. 74 <-notifyCh 75 if err := ep.GetSockOpt(tcpip.ErrorOption{}); err != tcpip.ErrAborted { 76 t.Fatalf("got ep.GetSockOpt(tcpip.ErrorOption{}) = %v, want = %v", err, tcpip.ErrAborted) 77 } 78 } 79 80 func TestConnectIncrementActiveConnection(t *testing.T) { 81 c := context.New(t, defaultMTU) 82 defer c.Cleanup() 83 84 stats := c.Stack().Stats() 85 want := stats.TCP.ActiveConnectionOpenings.Value() + 1 86 87 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 88 if got := stats.TCP.ActiveConnectionOpenings.Value(); got != want { 89 t.Errorf("got stats.TCP.ActtiveConnectionOpenings.Value() = %v, want = %v", got, want) 90 } 91 } 92 93 func TestConnectDoesNotIncrementFailedConnectionAttempts(t *testing.T) { 94 c := context.New(t, defaultMTU) 95 defer c.Cleanup() 96 97 stats := c.Stack().Stats() 98 want := stats.TCP.FailedConnectionAttempts.Value() 99 100 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 101 if got := stats.TCP.FailedConnectionAttempts.Value(); got != want { 102 t.Errorf("got stats.TCP.FailedConnectionAttempts.Value() = %v, want = %v", got, want) 103 } 104 if got := c.EP.Stats().(*tcp.Stats).FailedConnectionAttempts.Value(); got != want { 105 t.Errorf("got EP stats.FailedConnectionAttempts = %v, want = %v", got, want) 106 } 107 } 108 109 func TestActiveFailedConnectionAttemptIncrement(t *testing.T) { 110 c := context.New(t, defaultMTU) 111 defer c.Cleanup() 112 113 stats := c.Stack().Stats() 114 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ) 115 if err != nil { 116 t.Fatalf("NewEndpoint failed: %v", err) 117 } 118 c.EP = ep 119 want := stats.TCP.FailedConnectionAttempts.Value() + 1 120 121 if err := c.EP.Connect(tcpip.FullAddress{NIC: 2, Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrNoRoute { 122 t.Errorf("got c.EP.Connect(...) = %v, want = %v", err, tcpip.ErrNoRoute) 123 } 124 125 if got := stats.TCP.FailedConnectionAttempts.Value(); got != want { 126 t.Errorf("got stats.TCP.FailedConnectionAttempts.Value() = %v, want = %v", got, want) 127 } 128 if got := c.EP.Stats().(*tcp.Stats).FailedConnectionAttempts.Value(); got != want { 129 t.Errorf("got EP stats FailedConnectionAttempts = %v, want = %v", got, want) 130 } 131 } 132 133 func TestTCPSegmentsSentIncrement(t *testing.T) { 134 c := context.New(t, defaultMTU) 135 defer c.Cleanup() 136 137 stats := c.Stack().Stats() 138 // SYN and ACK 139 want := stats.TCP.SegmentsSent.Value() + 2 140 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 141 142 if got := stats.TCP.SegmentsSent.Value(); got != want { 143 t.Errorf("got stats.TCP.SegmentsSent.Value() = %v, want = %v", got, want) 144 } 145 if got := c.EP.Stats().(*tcp.Stats).SegmentsSent.Value(); got != want { 146 t.Errorf("got EP stats SegmentsSent.Value() = %v, want = %v", got, want) 147 } 148 } 149 150 func TestTCPResetsSentIncrement(t *testing.T) { 151 c := context.New(t, defaultMTU) 152 defer c.Cleanup() 153 stats := c.Stack().Stats() 154 wq := &waiter.Queue{} 155 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq) 156 if err != nil { 157 t.Fatalf("NewEndpoint failed: %v", err) 158 } 159 want := stats.TCP.SegmentsSent.Value() + 1 160 161 if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 162 t.Fatalf("Bind failed: %v", err) 163 } 164 165 if err := ep.Listen(10); err != nil { 166 t.Fatalf("Listen failed: %v", err) 167 } 168 169 // Send a SYN request. 170 iss := seqnum.Value(789) 171 c.SendPacket(nil, &context.Headers{ 172 SrcPort: context.TestPort, 173 DstPort: context.StackPort, 174 Flags: header.TCPFlagSyn, 175 SeqNum: iss, 176 }) 177 178 // Receive the SYN-ACK reply. 179 b := c.GetPacket() 180 tcpHdr := header.TCP(header.IPv4(b).Payload()) 181 c.IRS = seqnum.Value(tcpHdr.SequenceNumber()) 182 183 ackHeaders := &context.Headers{ 184 SrcPort: context.TestPort, 185 DstPort: context.StackPort, 186 Flags: header.TCPFlagAck, 187 SeqNum: iss + 1, 188 // If the AckNum is not the increment of the last sequence number, a RST 189 // segment is sent back in response. 190 AckNum: c.IRS + 2, 191 } 192 193 // Send ACK. 194 c.SendPacket(nil, ackHeaders) 195 196 c.GetPacket() 197 if got := stats.TCP.ResetsSent.Value(); got != want { 198 t.Errorf("got stats.TCP.ResetsSent.Value() = %v, want = %v", got, want) 199 } 200 } 201 202 // TestTCPResetSentForACKWhenNotUsingSynCookies checks that the stack generates 203 // a RST if an ACK is received on the listening socket for which there is no 204 // active handshake in progress and we are not using SYN cookies. 205 func TestTCPResetSentForACKWhenNotUsingSynCookies(t *testing.T) { 206 c := context.New(t, defaultMTU) 207 defer c.Cleanup() 208 209 // Set TCPLingerTimeout to 5 seconds so that sockets are marked closed 210 wq := &waiter.Queue{} 211 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq) 212 if err != nil { 213 t.Fatalf("NewEndpoint failed: %s", err) 214 } 215 if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 216 t.Fatalf("Bind failed: %s", err) 217 } 218 219 if err := ep.Listen(10); err != nil { 220 t.Fatalf("Listen failed: %s", err) 221 } 222 223 // Send a SYN request. 224 iss := seqnum.Value(789) 225 c.SendPacket(nil, &context.Headers{ 226 SrcPort: context.TestPort, 227 DstPort: context.StackPort, 228 Flags: header.TCPFlagSyn, 229 SeqNum: iss, 230 }) 231 232 // Receive the SYN-ACK reply. 233 b := c.GetPacket() 234 tcpHdr := header.TCP(header.IPv4(b).Payload()) 235 c.IRS = seqnum.Value(tcpHdr.SequenceNumber()) 236 237 ackHeaders := &context.Headers{ 238 SrcPort: context.TestPort, 239 DstPort: context.StackPort, 240 Flags: header.TCPFlagAck, 241 SeqNum: iss + 1, 242 AckNum: c.IRS + 1, 243 } 244 245 // Send ACK. 246 c.SendPacket(nil, ackHeaders) 247 248 // Try to accept the connection. 249 we, ch := waiter.NewChannelEntry(nil) 250 wq.EventRegister(&we, waiter.EventIn) 251 defer wq.EventUnregister(&we) 252 253 c.EP, _, err = ep.Accept() 254 if err == tcpip.ErrWouldBlock { 255 // Wait for connection to be established. 256 select { 257 case <-ch: 258 c.EP, _, err = ep.Accept() 259 if err != nil { 260 t.Fatalf("Accept failed: %s", err) 261 } 262 263 case <-time.After(1 * time.Second): 264 t.Fatalf("Timed out waiting for accept") 265 } 266 } 267 268 // Lower stackwide TIME_WAIT timeout so that the reservations 269 // are released instantly on Close. 270 tcpTW := tcpip.TCPTimeWaitTimeoutOption(1 * time.Millisecond) 271 if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, tcpTW); err != nil { 272 t.Fatalf("e.stack.SetTransportProtocolOption(%d, %s) = %s", tcp.ProtocolNumber, tcpTW, err) 273 } 274 275 c.EP.Close() 276 checker.IPv4(t, c.GetPacket(), checker.TCP( 277 checker.SrcPort(context.StackPort), 278 checker.DstPort(context.TestPort), 279 checker.SeqNum(uint32(c.IRS+1)), 280 checker.AckNum(uint32(iss)+1), 281 checker.TCPFlags(header.TCPFlagFin|header.TCPFlagAck))) 282 283 finHeaders := &context.Headers{ 284 SrcPort: context.TestPort, 285 DstPort: context.StackPort, 286 Flags: header.TCPFlagAck | header.TCPFlagFin, 287 SeqNum: iss + 1, 288 AckNum: c.IRS + 2, 289 } 290 291 c.SendPacket(nil, finHeaders) 292 293 // Get the ACK to the FIN we just sent. 294 c.GetPacket() 295 296 // Since an active close was done we need to wait for a little more than 297 // tcpLingerTimeout for the port reservations to be released and the 298 // socket to move to a CLOSED state. 299 time.Sleep(20 * time.Millisecond) 300 301 // Now resend the same ACK, this ACK should generate a RST as there 302 // should be no endpoint in SYN-RCVD state and we are not using 303 // syn-cookies yet. The reason we send the same ACK is we need a valid 304 // cookie(IRS) generated by the netstack without which the ACK will be 305 // rejected. 306 c.SendPacket(nil, ackHeaders) 307 308 checker.IPv4(t, c.GetPacket(), checker.TCP( 309 checker.SrcPort(context.StackPort), 310 checker.DstPort(context.TestPort), 311 checker.SeqNum(uint32(c.IRS+1)), 312 checker.AckNum(uint32(iss)+1), 313 checker.TCPFlags(header.TCPFlagRst|header.TCPFlagAck))) 314 } 315 316 func TestTCPResetsReceivedIncrement(t *testing.T) { 317 c := context.New(t, defaultMTU) 318 defer c.Cleanup() 319 320 stats := c.Stack().Stats() 321 want := stats.TCP.ResetsReceived.Value() + 1 322 iss := seqnum.Value(789) 323 rcvWnd := seqnum.Size(30000) 324 c.CreateConnected(iss, rcvWnd, -1 /* epRcvBuf */) 325 326 c.SendPacket(nil, &context.Headers{ 327 SrcPort: context.TestPort, 328 DstPort: c.Port, 329 SeqNum: iss.Add(1), 330 AckNum: c.IRS.Add(1), 331 RcvWnd: rcvWnd, 332 Flags: header.TCPFlagRst, 333 }) 334 335 if got := stats.TCP.ResetsReceived.Value(); got != want { 336 t.Errorf("got stats.TCP.ResetsReceived.Value() = %v, want = %v", got, want) 337 } 338 } 339 340 func TestTCPResetsDoNotGenerateResets(t *testing.T) { 341 c := context.New(t, defaultMTU) 342 defer c.Cleanup() 343 344 stats := c.Stack().Stats() 345 want := stats.TCP.ResetsReceived.Value() + 1 346 iss := seqnum.Value(789) 347 rcvWnd := seqnum.Size(30000) 348 c.CreateConnected(iss, rcvWnd, -1 /* epRcvBuf */) 349 350 c.SendPacket(nil, &context.Headers{ 351 SrcPort: context.TestPort, 352 DstPort: c.Port, 353 SeqNum: iss.Add(1), 354 AckNum: c.IRS.Add(1), 355 RcvWnd: rcvWnd, 356 Flags: header.TCPFlagRst, 357 }) 358 359 if got := stats.TCP.ResetsReceived.Value(); got != want { 360 t.Errorf("got stats.TCP.ResetsReceived.Value() = %v, want = %v", got, want) 361 } 362 c.CheckNoPacketTimeout("got an unexpected packet", 100*time.Millisecond) 363 } 364 365 func TestActiveHandshake(t *testing.T) { 366 c := context.New(t, defaultMTU) 367 defer c.Cleanup() 368 369 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 370 } 371 372 func TestNonBlockingClose(t *testing.T) { 373 c := context.New(t, defaultMTU) 374 defer c.Cleanup() 375 376 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 377 ep := c.EP 378 c.EP = nil 379 380 // Close the endpoint and measure how long it takes. 381 t0 := time.Now() 382 ep.Close() 383 if diff := time.Now().Sub(t0); diff > 3*time.Second { 384 t.Fatalf("Took too long to close: %v", diff) 385 } 386 } 387 388 func TestConnectResetAfterClose(t *testing.T) { 389 c := context.New(t, defaultMTU) 390 defer c.Cleanup() 391 392 // Set TCPLinger to 3 seconds so that sockets are marked closed 393 // after 3 second in FIN_WAIT2 state. 394 tcpLingerTimeout := 3 * time.Second 395 if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, tcpip.TCPLingerTimeoutOption(tcpLingerTimeout)); err != nil { 396 t.Fatalf("c.stack.SetTransportProtocolOption(tcp, tcpip.TCPLingerTimeoutOption(%d) failed: %s", tcpLingerTimeout, err) 397 } 398 399 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 400 ep := c.EP 401 c.EP = nil 402 403 // Close the endpoint, make sure we get a FIN segment, then acknowledge 404 // to complete closure of sender, but don't send our own FIN. 405 ep.Close() 406 checker.IPv4(t, c.GetPacket(), 407 checker.TCP( 408 checker.DstPort(context.TestPort), 409 checker.SeqNum(uint32(c.IRS)+1), 410 checker.AckNum(790), 411 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin), 412 ), 413 ) 414 c.SendPacket(nil, &context.Headers{ 415 SrcPort: context.TestPort, 416 DstPort: c.Port, 417 Flags: header.TCPFlagAck, 418 SeqNum: 790, 419 AckNum: c.IRS.Add(2), 420 RcvWnd: 30000, 421 }) 422 423 // Wait for the ep to give up waiting for a FIN. 424 time.Sleep(tcpLingerTimeout + 1*time.Second) 425 426 // Now send an ACK and it should trigger a RST as the endpoint should 427 // not exist anymore. 428 c.SendPacket(nil, &context.Headers{ 429 SrcPort: context.TestPort, 430 DstPort: c.Port, 431 Flags: header.TCPFlagAck, 432 SeqNum: 790, 433 AckNum: c.IRS.Add(2), 434 RcvWnd: 30000, 435 }) 436 437 for { 438 b := c.GetPacket() 439 tcpHdr := header.TCP(header.IPv4(b).Payload()) 440 if tcpHdr.Flags() == header.TCPFlagAck|header.TCPFlagFin { 441 // This is a retransmit of the FIN, ignore it. 442 continue 443 } 444 445 checker.IPv4(t, b, 446 checker.TCP( 447 checker.DstPort(context.TestPort), 448 checker.SeqNum(uint32(c.IRS)+2), 449 checker.AckNum(790), 450 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagRst), 451 ), 452 ) 453 break 454 } 455 } 456 457 // TestClosingWithEnqueuedSegments tests handling of 458 // still enqueued segments when the endpoint transitions 459 // to StateClose. The in-flight segments would be re-enqueued 460 // to a any listening endpoint. 461 func TestClosingWithEnqueuedSegments(t *testing.T) { 462 c := context.New(t, defaultMTU) 463 defer c.Cleanup() 464 465 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 466 ep := c.EP 467 c.EP = nil 468 469 if got, want := tcp.EndpointState(ep.State()), tcp.StateEstablished; got != want { 470 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 471 } 472 473 // Send a FIN for ESTABLISHED --> CLOSED-WAIT 474 c.SendPacket(nil, &context.Headers{ 475 SrcPort: context.TestPort, 476 DstPort: c.Port, 477 Flags: header.TCPFlagFin | header.TCPFlagAck, 478 SeqNum: 790, 479 AckNum: c.IRS.Add(1), 480 RcvWnd: 30000, 481 }) 482 483 // Get the ACK for the FIN we sent. 484 checker.IPv4(t, c.GetPacket(), 485 checker.TCP( 486 checker.DstPort(context.TestPort), 487 checker.SeqNum(uint32(c.IRS)+1), 488 checker.AckNum(791), 489 checker.TCPFlags(header.TCPFlagAck), 490 ), 491 ) 492 493 if got, want := tcp.EndpointState(ep.State()), tcp.StateCloseWait; got != want { 494 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 495 } 496 497 // Close the application endpoint for CLOSE_WAIT --> LAST_ACK 498 ep.Close() 499 500 // Get the FIN 501 checker.IPv4(t, c.GetPacket(), 502 checker.TCP( 503 checker.DstPort(context.TestPort), 504 checker.SeqNum(uint32(c.IRS)+1), 505 checker.AckNum(791), 506 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin), 507 ), 508 ) 509 510 if got, want := tcp.EndpointState(ep.State()), tcp.StateLastAck; got != want { 511 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 512 } 513 514 // Pause the endpoint`s protocolMainLoop. 515 ep.(interface{ StopWork() }).StopWork() 516 517 // Enqueue last ACK followed by an ACK matching the endpoint 518 // 519 // Send Last ACK for LAST_ACK --> CLOSED 520 c.SendPacket(nil, &context.Headers{ 521 SrcPort: context.TestPort, 522 DstPort: c.Port, 523 Flags: header.TCPFlagAck, 524 SeqNum: 791, 525 AckNum: c.IRS.Add(2), 526 RcvWnd: 30000, 527 }) 528 529 // Send a packet with ACK set, this would generate RST when 530 // not using SYN cookies as in this test. 531 c.SendPacket(nil, &context.Headers{ 532 SrcPort: context.TestPort, 533 DstPort: c.Port, 534 Flags: header.TCPFlagAck | header.TCPFlagFin, 535 SeqNum: 792, 536 AckNum: c.IRS.Add(2), 537 RcvWnd: 30000, 538 }) 539 540 // Unpause endpoint`s protocolMainLoop. 541 ep.(interface{ ResumeWork() }).ResumeWork() 542 543 // Wait for the protocolMainLoop to resume and update state. 544 time.Sleep(1 * time.Millisecond) 545 546 // Expect the endpoint to be closed. 547 if got, want := tcp.EndpointState(ep.State()), tcp.StateClose; got != want { 548 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 549 } 550 551 // Check if the endpoint was moved to CLOSED and netstack a reset in 552 // response to the ACK packet that we sent after last-ACK. 553 checker.IPv4(t, c.GetPacket(), 554 checker.TCP( 555 checker.DstPort(context.TestPort), 556 checker.SeqNum(uint32(c.IRS)+2), 557 checker.AckNum(793), 558 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagRst), 559 ), 560 ) 561 } 562 563 func TestSimpleReceive(t *testing.T) { 564 c := context.New(t, defaultMTU) 565 defer c.Cleanup() 566 567 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 568 569 we, ch := waiter.NewChannelEntry(nil) 570 c.WQ.EventRegister(&we, waiter.EventIn) 571 defer c.WQ.EventUnregister(&we) 572 573 if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock { 574 t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock) 575 } 576 577 data := []byte{1, 2, 3} 578 c.SendPacket(data, &context.Headers{ 579 SrcPort: context.TestPort, 580 DstPort: c.Port, 581 Flags: header.TCPFlagAck, 582 SeqNum: 790, 583 AckNum: c.IRS.Add(1), 584 RcvWnd: 30000, 585 }) 586 587 // Wait for receive to be notified. 588 select { 589 case <-ch: 590 case <-time.After(1 * time.Second): 591 t.Fatalf("Timed out waiting for data to arrive") 592 } 593 594 // Receive data. 595 v, _, err := c.EP.Read(nil) 596 if err != nil { 597 t.Fatalf("Read failed: %v", err) 598 } 599 600 if !bytes.Equal(data, v) { 601 t.Fatalf("got data = %v, want = %v", v, data) 602 } 603 604 // Check that ACK is received. 605 checker.IPv4(t, c.GetPacket(), 606 checker.TCP( 607 checker.DstPort(context.TestPort), 608 checker.SeqNum(uint32(c.IRS)+1), 609 checker.AckNum(uint32(790+len(data))), 610 checker.TCPFlags(header.TCPFlagAck), 611 ), 612 ) 613 } 614 615 // TestUserSuppliedMSSOnConnectV4 tests that the user supplied MSS is used when 616 // creating a new active IPv4 TCP socket. It should be present in the sent TCP 617 // SYN segment. 618 func TestUserSuppliedMSSOnConnectV4(t *testing.T) { 619 const mtu = 5000 620 const maxMSS = mtu - header.IPv4MinimumSize - header.TCPMinimumSize 621 tests := []struct { 622 name string 623 setMSS uint16 624 expMSS uint16 625 }{ 626 { 627 "EqualToMaxMSS", 628 maxMSS, 629 maxMSS, 630 }, 631 { 632 "LessThanMTU", 633 maxMSS - 1, 634 maxMSS - 1, 635 }, 636 { 637 "GreaterThanMTU", 638 maxMSS + 1, 639 maxMSS, 640 }, 641 } 642 643 for _, test := range tests { 644 t.Run(test.name, func(t *testing.T) { 645 c := context.New(t, mtu) 646 defer c.Cleanup() 647 648 c.Create(-1) 649 650 // Set the MSS socket option. 651 opt := tcpip.MaxSegOption(test.setMSS) 652 if err := c.EP.SetSockOpt(opt); err != nil { 653 t.Fatalf("SetSockOpt(%#v) failed: %s", opt, err) 654 } 655 656 // Get expected window size. 657 rcvBufSize, err := c.EP.GetSockOptInt(tcpip.ReceiveBufferSizeOption) 658 if err != nil { 659 t.Fatalf("GetSockOpt(%v) failed: %s", tcpip.ReceiveBufferSizeOption, err) 660 } 661 ws := tcp.FindWndScale(seqnum.Size(rcvBufSize)) 662 663 // Start connection attempt to IPv4 address. 664 if err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrConnectStarted { 665 t.Fatalf("Unexpected return value from Connect: %v", err) 666 } 667 668 // Receive SYN packet with our user supplied MSS. 669 checker.IPv4(t, c.GetPacket(), checker.TCP( 670 checker.DstPort(context.TestPort), 671 checker.TCPFlags(header.TCPFlagSyn), 672 checker.TCPSynOptions(header.TCPSynOptions{MSS: test.expMSS, WS: ws}))) 673 }) 674 } 675 } 676 677 // TestUserSuppliedMSSOnConnectV6 tests that the user supplied MSS is used when 678 // creating a new active IPv6 TCP socket. It should be present in the sent TCP 679 // SYN segment. 680 func TestUserSuppliedMSSOnConnectV6(t *testing.T) { 681 const mtu = 5000 682 const maxMSS = mtu - header.IPv6MinimumSize - header.TCPMinimumSize 683 tests := []struct { 684 name string 685 setMSS uint16 686 expMSS uint16 687 }{ 688 { 689 "EqualToMaxMSS", 690 maxMSS, 691 maxMSS, 692 }, 693 { 694 "LessThanMTU", 695 maxMSS - 1, 696 maxMSS - 1, 697 }, 698 { 699 "GreaterThanMTU", 700 maxMSS + 1, 701 maxMSS, 702 }, 703 } 704 705 for _, test := range tests { 706 t.Run(test.name, func(t *testing.T) { 707 c := context.New(t, mtu) 708 defer c.Cleanup() 709 710 c.CreateV6Endpoint(true) 711 712 // Set the MSS socket option. 713 opt := tcpip.MaxSegOption(test.setMSS) 714 if err := c.EP.SetSockOpt(opt); err != nil { 715 t.Fatalf("SetSockOpt(%#v) failed: %s", opt, err) 716 } 717 718 // Get expected window size. 719 rcvBufSize, err := c.EP.GetSockOptInt(tcpip.ReceiveBufferSizeOption) 720 if err != nil { 721 t.Fatalf("GetSockOpt(%v) failed: %s", tcpip.ReceiveBufferSizeOption, err) 722 } 723 ws := tcp.FindWndScale(seqnum.Size(rcvBufSize)) 724 725 // Start connection attempt to IPv6 address. 726 if err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestV6Addr, Port: context.TestPort}); err != tcpip.ErrConnectStarted { 727 t.Fatalf("Unexpected return value from Connect: %v", err) 728 } 729 730 // Receive SYN packet with our user supplied MSS. 731 checker.IPv6(t, c.GetV6Packet(), checker.TCP( 732 checker.DstPort(context.TestPort), 733 checker.TCPFlags(header.TCPFlagSyn), 734 checker.TCPSynOptions(header.TCPSynOptions{MSS: test.expMSS, WS: ws}))) 735 }) 736 } 737 } 738 739 func TestSendRstOnListenerRxSynAckV4(t *testing.T) { 740 c := context.New(t, defaultMTU) 741 defer c.Cleanup() 742 743 c.Create(-1) 744 745 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 746 t.Fatal("Bind failed:", err) 747 } 748 749 if err := c.EP.Listen(10); err != nil { 750 t.Fatal("Listen failed:", err) 751 } 752 753 c.SendPacket(nil, &context.Headers{ 754 SrcPort: context.TestPort, 755 DstPort: context.StackPort, 756 Flags: header.TCPFlagSyn | header.TCPFlagAck, 757 SeqNum: 100, 758 AckNum: 200, 759 }) 760 761 checker.IPv4(t, c.GetPacket(), checker.TCP( 762 checker.DstPort(context.TestPort), 763 checker.TCPFlags(header.TCPFlagRst), 764 checker.SeqNum(200))) 765 } 766 767 func TestSendRstOnListenerRxSynAckV6(t *testing.T) { 768 c := context.New(t, defaultMTU) 769 defer c.Cleanup() 770 771 c.CreateV6Endpoint(true) 772 773 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 774 t.Fatal("Bind failed:", err) 775 } 776 777 if err := c.EP.Listen(10); err != nil { 778 t.Fatal("Listen failed:", err) 779 } 780 781 c.SendV6Packet(nil, &context.Headers{ 782 SrcPort: context.TestPort, 783 DstPort: context.StackPort, 784 Flags: header.TCPFlagSyn | header.TCPFlagAck, 785 SeqNum: 100, 786 AckNum: 200, 787 }) 788 789 checker.IPv6(t, c.GetV6Packet(), checker.TCP( 790 checker.DstPort(context.TestPort), 791 checker.TCPFlags(header.TCPFlagRst), 792 checker.SeqNum(200))) 793 } 794 795 func TestSendRstOnListenerRxAckV4(t *testing.T) { 796 c := context.New(t, defaultMTU) 797 defer c.Cleanup() 798 799 c.Create(-1 /* epRcvBuf */) 800 801 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 802 t.Fatal("Bind failed:", err) 803 } 804 805 if err := c.EP.Listen(10 /* backlog */); err != nil { 806 t.Fatal("Listen failed:", err) 807 } 808 809 c.SendPacket(nil, &context.Headers{ 810 SrcPort: context.TestPort, 811 DstPort: context.StackPort, 812 Flags: header.TCPFlagFin | header.TCPFlagAck, 813 SeqNum: 100, 814 AckNum: 200, 815 }) 816 817 checker.IPv4(t, c.GetPacket(), checker.TCP( 818 checker.DstPort(context.TestPort), 819 checker.TCPFlags(header.TCPFlagRst|header.TCPFlagAck), 820 checker.SeqNum(200))) 821 } 822 823 func TestSendRstOnListenerRxAckV6(t *testing.T) { 824 c := context.New(t, defaultMTU) 825 defer c.Cleanup() 826 827 c.CreateV6Endpoint(true /* v6Only */) 828 829 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 830 t.Fatal("Bind failed:", err) 831 } 832 833 if err := c.EP.Listen(10 /* backlog */); err != nil { 834 t.Fatal("Listen failed:", err) 835 } 836 837 c.SendV6Packet(nil, &context.Headers{ 838 SrcPort: context.TestPort, 839 DstPort: context.StackPort, 840 Flags: header.TCPFlagFin | header.TCPFlagAck, 841 SeqNum: 100, 842 AckNum: 200, 843 }) 844 845 checker.IPv6(t, c.GetV6Packet(), checker.TCP( 846 checker.DstPort(context.TestPort), 847 checker.TCPFlags(header.TCPFlagRst|header.TCPFlagAck), 848 checker.SeqNum(200))) 849 } 850 851 // TestListenShutdown tests for the listening endpoint not processing 852 // any receive when it is on read shutdown. 853 func TestListenShutdown(t *testing.T) { 854 c := context.New(t, defaultMTU) 855 defer c.Cleanup() 856 857 c.Create(-1 /* epRcvBuf */) 858 859 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 860 t.Fatal("Bind failed:", err) 861 } 862 863 if err := c.EP.Listen(10 /* backlog */); err != nil { 864 t.Fatal("Listen failed:", err) 865 } 866 867 if err := c.EP.Shutdown(tcpip.ShutdownRead); err != nil { 868 t.Fatal("Shutdown failed:", err) 869 } 870 871 // Wait for the endpoint state to be propagated. 872 time.Sleep(10 * time.Millisecond) 873 874 c.SendPacket(nil, &context.Headers{ 875 SrcPort: context.TestPort, 876 DstPort: context.StackPort, 877 Flags: header.TCPFlagSyn, 878 SeqNum: 100, 879 AckNum: 200, 880 }) 881 882 c.CheckNoPacket("Packet received when listening socket was shutdown") 883 } 884 885 func TestTOSV4(t *testing.T) { 886 c := context.New(t, defaultMTU) 887 defer c.Cleanup() 888 889 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ) 890 if err != nil { 891 t.Fatalf("NewEndpoint failed: %s", err) 892 } 893 c.EP = ep 894 895 const tos = 0xC0 896 if err := c.EP.SetSockOpt(tcpip.IPv4TOSOption(tos)); err != nil { 897 t.Errorf("SetSockOpt(%#v) failed: %s", tcpip.IPv4TOSOption(tos), err) 898 } 899 900 var v tcpip.IPv4TOSOption 901 if err := c.EP.GetSockOpt(&v); err != nil { 902 t.Errorf("GetSockopt failed: %s", err) 903 } 904 905 if want := tcpip.IPv4TOSOption(tos); v != want { 906 t.Errorf("got GetSockOpt(...) = %#v, want = %#v", v, want) 907 } 908 909 testV4Connect(t, c, checker.TOS(tos, 0)) 910 911 data := []byte{1, 2, 3} 912 view := buffer.NewView(len(data)) 913 copy(view, data) 914 915 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 916 t.Fatalf("Write failed: %s", err) 917 } 918 919 // Check that data is received. 920 b := c.GetPacket() 921 checker.IPv4(t, b, 922 checker.PayloadLen(len(data)+header.TCPMinimumSize), 923 checker.TCP( 924 checker.DstPort(context.TestPort), 925 checker.SeqNum(uint32(c.IRS)+1), 926 checker.AckNum(790), // Acknum is initial sequence number + 1 927 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 928 ), 929 checker.TOS(tos, 0), 930 ) 931 932 if p := b[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(data, p) { 933 t.Errorf("got data = %x, want = %x", p, data) 934 } 935 } 936 937 func TestTrafficClassV6(t *testing.T) { 938 c := context.New(t, defaultMTU) 939 defer c.Cleanup() 940 941 c.CreateV6Endpoint(false) 942 943 const tos = 0xC0 944 if err := c.EP.SetSockOpt(tcpip.IPv6TrafficClassOption(tos)); err != nil { 945 t.Errorf("SetSockOpt(%#v) failed: %s", tcpip.IPv6TrafficClassOption(tos), err) 946 } 947 948 var v tcpip.IPv6TrafficClassOption 949 if err := c.EP.GetSockOpt(&v); err != nil { 950 t.Fatalf("GetSockopt failed: %s", err) 951 } 952 953 if want := tcpip.IPv6TrafficClassOption(tos); v != want { 954 t.Errorf("got GetSockOpt(...) = %#v, want = %#v", v, want) 955 } 956 957 // Test the connection request. 958 testV6Connect(t, c, checker.TOS(tos, 0)) 959 960 data := []byte{1, 2, 3} 961 view := buffer.NewView(len(data)) 962 copy(view, data) 963 964 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 965 t.Fatalf("Write failed: %s", err) 966 } 967 968 // Check that data is received. 969 b := c.GetV6Packet() 970 checker.IPv6(t, b, 971 checker.PayloadLen(len(data)+header.TCPMinimumSize), 972 checker.TCP( 973 checker.DstPort(context.TestPort), 974 checker.SeqNum(uint32(c.IRS)+1), 975 checker.AckNum(790), 976 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 977 ), 978 checker.TOS(tos, 0), 979 ) 980 981 if p := b[header.IPv6MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(data, p) { 982 t.Errorf("got data = %x, want = %x", p, data) 983 } 984 } 985 986 func TestConnectBindToDevice(t *testing.T) { 987 for _, test := range []struct { 988 name string 989 device string 990 want tcp.EndpointState 991 }{ 992 {"RightDevice", "nic1", tcp.StateEstablished}, 993 {"WrongDevice", "nic2", tcp.StateSynSent}, 994 {"AnyDevice", "", tcp.StateEstablished}, 995 } { 996 t.Run(test.name, func(t *testing.T) { 997 c := context.New(t, defaultMTU) 998 defer c.Cleanup() 999 1000 c.Create(-1) 1001 bindToDevice := tcpip.BindToDeviceOption(test.device) 1002 c.EP.SetSockOpt(bindToDevice) 1003 // Start connection attempt. 1004 waitEntry, _ := waiter.NewChannelEntry(nil) 1005 c.WQ.EventRegister(&waitEntry, waiter.EventOut) 1006 defer c.WQ.EventUnregister(&waitEntry) 1007 1008 if err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrConnectStarted { 1009 t.Fatalf("Unexpected return value from Connect: %v", err) 1010 } 1011 1012 // Receive SYN packet. 1013 b := c.GetPacket() 1014 checker.IPv4(t, b, 1015 checker.TCP( 1016 checker.DstPort(context.TestPort), 1017 checker.TCPFlags(header.TCPFlagSyn), 1018 ), 1019 ) 1020 if got, want := tcp.EndpointState(c.EP.State()), tcp.StateSynSent; got != want { 1021 t.Fatalf("Unexpected endpoint state: want %v, got %v", want, got) 1022 } 1023 tcpHdr := header.TCP(header.IPv4(b).Payload()) 1024 c.IRS = seqnum.Value(tcpHdr.SequenceNumber()) 1025 1026 iss := seqnum.Value(789) 1027 rcvWnd := seqnum.Size(30000) 1028 c.SendPacket(nil, &context.Headers{ 1029 SrcPort: tcpHdr.DestinationPort(), 1030 DstPort: tcpHdr.SourcePort(), 1031 Flags: header.TCPFlagSyn | header.TCPFlagAck, 1032 SeqNum: iss, 1033 AckNum: c.IRS.Add(1), 1034 RcvWnd: rcvWnd, 1035 TCPOpts: nil, 1036 }) 1037 1038 c.GetPacket() 1039 if got, want := tcp.EndpointState(c.EP.State()), test.want; got != want { 1040 t.Fatalf("Unexpected endpoint state: want %v, got %v", want, got) 1041 } 1042 }) 1043 } 1044 } 1045 1046 func TestOutOfOrderReceive(t *testing.T) { 1047 c := context.New(t, defaultMTU) 1048 defer c.Cleanup() 1049 1050 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 1051 1052 we, ch := waiter.NewChannelEntry(nil) 1053 c.WQ.EventRegister(&we, waiter.EventIn) 1054 defer c.WQ.EventUnregister(&we) 1055 1056 if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock { 1057 t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock) 1058 } 1059 1060 // Send second half of data first, with seqnum 3 ahead of expected. 1061 data := []byte{1, 2, 3, 4, 5, 6} 1062 c.SendPacket(data[3:], &context.Headers{ 1063 SrcPort: context.TestPort, 1064 DstPort: c.Port, 1065 Flags: header.TCPFlagAck, 1066 SeqNum: 793, 1067 AckNum: c.IRS.Add(1), 1068 RcvWnd: 30000, 1069 }) 1070 1071 // Check that we get an ACK specifying which seqnum is expected. 1072 checker.IPv4(t, c.GetPacket(), 1073 checker.TCP( 1074 checker.DstPort(context.TestPort), 1075 checker.SeqNum(uint32(c.IRS)+1), 1076 checker.AckNum(790), 1077 checker.TCPFlags(header.TCPFlagAck), 1078 ), 1079 ) 1080 1081 // Wait 200ms and check that no data has been received. 1082 time.Sleep(200 * time.Millisecond) 1083 if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock { 1084 t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock) 1085 } 1086 1087 // Send the first 3 bytes now. 1088 c.SendPacket(data[:3], &context.Headers{ 1089 SrcPort: context.TestPort, 1090 DstPort: c.Port, 1091 Flags: header.TCPFlagAck, 1092 SeqNum: 790, 1093 AckNum: c.IRS.Add(1), 1094 RcvWnd: 30000, 1095 }) 1096 1097 // Receive data. 1098 read := make([]byte, 0, 6) 1099 for len(read) < len(data) { 1100 v, _, err := c.EP.Read(nil) 1101 if err != nil { 1102 if err == tcpip.ErrWouldBlock { 1103 // Wait for receive to be notified. 1104 select { 1105 case <-ch: 1106 case <-time.After(5 * time.Second): 1107 t.Fatalf("Timed out waiting for data to arrive") 1108 } 1109 continue 1110 } 1111 t.Fatalf("Read failed: %v", err) 1112 } 1113 1114 read = append(read, v...) 1115 } 1116 1117 // Check that we received the data in proper order. 1118 if !bytes.Equal(data, read) { 1119 t.Fatalf("got data = %v, want = %v", read, data) 1120 } 1121 1122 // Check that the whole data is acknowledged. 1123 checker.IPv4(t, c.GetPacket(), 1124 checker.TCP( 1125 checker.DstPort(context.TestPort), 1126 checker.SeqNum(uint32(c.IRS)+1), 1127 checker.AckNum(uint32(790+len(data))), 1128 checker.TCPFlags(header.TCPFlagAck), 1129 ), 1130 ) 1131 } 1132 1133 func TestOutOfOrderFlood(t *testing.T) { 1134 c := context.New(t, defaultMTU) 1135 defer c.Cleanup() 1136 1137 // Create a new connection with initial window size of 10. 1138 c.CreateConnected(789, 30000, 10) 1139 1140 if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock { 1141 t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock) 1142 } 1143 1144 // Send 100 packets before the actual one that is expected. 1145 data := []byte{1, 2, 3, 4, 5, 6} 1146 for i := 0; i < 100; i++ { 1147 c.SendPacket(data[3:], &context.Headers{ 1148 SrcPort: context.TestPort, 1149 DstPort: c.Port, 1150 Flags: header.TCPFlagAck, 1151 SeqNum: 796, 1152 AckNum: c.IRS.Add(1), 1153 RcvWnd: 30000, 1154 }) 1155 1156 checker.IPv4(t, c.GetPacket(), 1157 checker.TCP( 1158 checker.DstPort(context.TestPort), 1159 checker.SeqNum(uint32(c.IRS)+1), 1160 checker.AckNum(790), 1161 checker.TCPFlags(header.TCPFlagAck), 1162 ), 1163 ) 1164 } 1165 1166 // Send packet with seqnum 793. It must be discarded because the 1167 // out-of-order buffer was filled by the previous packets. 1168 c.SendPacket(data[3:], &context.Headers{ 1169 SrcPort: context.TestPort, 1170 DstPort: c.Port, 1171 Flags: header.TCPFlagAck, 1172 SeqNum: 793, 1173 AckNum: c.IRS.Add(1), 1174 RcvWnd: 30000, 1175 }) 1176 1177 checker.IPv4(t, c.GetPacket(), 1178 checker.TCP( 1179 checker.DstPort(context.TestPort), 1180 checker.SeqNum(uint32(c.IRS)+1), 1181 checker.AckNum(790), 1182 checker.TCPFlags(header.TCPFlagAck), 1183 ), 1184 ) 1185 1186 // Now send the expected packet, seqnum 790. 1187 c.SendPacket(data[:3], &context.Headers{ 1188 SrcPort: context.TestPort, 1189 DstPort: c.Port, 1190 Flags: header.TCPFlagAck, 1191 SeqNum: 790, 1192 AckNum: c.IRS.Add(1), 1193 RcvWnd: 30000, 1194 }) 1195 1196 // Check that only packet 790 is acknowledged. 1197 checker.IPv4(t, c.GetPacket(), 1198 checker.TCP( 1199 checker.DstPort(context.TestPort), 1200 checker.SeqNum(uint32(c.IRS)+1), 1201 checker.AckNum(793), 1202 checker.TCPFlags(header.TCPFlagAck), 1203 ), 1204 ) 1205 } 1206 1207 func TestRstOnCloseWithUnreadData(t *testing.T) { 1208 c := context.New(t, defaultMTU) 1209 defer c.Cleanup() 1210 1211 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 1212 1213 we, ch := waiter.NewChannelEntry(nil) 1214 c.WQ.EventRegister(&we, waiter.EventIn) 1215 defer c.WQ.EventUnregister(&we) 1216 1217 if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock { 1218 t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock) 1219 } 1220 1221 data := []byte{1, 2, 3} 1222 c.SendPacket(data, &context.Headers{ 1223 SrcPort: context.TestPort, 1224 DstPort: c.Port, 1225 Flags: header.TCPFlagAck, 1226 SeqNum: 790, 1227 AckNum: c.IRS.Add(1), 1228 RcvWnd: 30000, 1229 }) 1230 1231 // Wait for receive to be notified. 1232 select { 1233 case <-ch: 1234 case <-time.After(3 * time.Second): 1235 t.Fatalf("Timed out waiting for data to arrive") 1236 } 1237 1238 // Check that ACK is received, this happens regardless of the read. 1239 checker.IPv4(t, c.GetPacket(), 1240 checker.TCP( 1241 checker.DstPort(context.TestPort), 1242 checker.SeqNum(uint32(c.IRS)+1), 1243 checker.AckNum(uint32(790+len(data))), 1244 checker.TCPFlags(header.TCPFlagAck), 1245 ), 1246 ) 1247 1248 // Now that we know we have unread data, let's just close the connection 1249 // and verify that netstack sends an RST rather than a FIN. 1250 c.EP.Close() 1251 1252 checker.IPv4(t, c.GetPacket(), 1253 checker.TCP( 1254 checker.DstPort(context.TestPort), 1255 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagRst), 1256 // We shouldn't consume a sequence number on RST. 1257 checker.SeqNum(uint32(c.IRS)+1), 1258 )) 1259 // The RST puts the endpoint into an error state. 1260 if got, want := tcp.EndpointState(c.EP.State()), tcp.StateError; got != want { 1261 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 1262 } 1263 1264 // This final ACK should be ignored because an ACK on a reset doesn't mean 1265 // anything. 1266 c.SendPacket(nil, &context.Headers{ 1267 SrcPort: context.TestPort, 1268 DstPort: c.Port, 1269 Flags: header.TCPFlagAck, 1270 SeqNum: seqnum.Value(790 + len(data)), 1271 AckNum: c.IRS.Add(seqnum.Size(2)), 1272 RcvWnd: 30000, 1273 }) 1274 } 1275 1276 func TestRstOnCloseWithUnreadDataFinConvertRst(t *testing.T) { 1277 c := context.New(t, defaultMTU) 1278 defer c.Cleanup() 1279 1280 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 1281 1282 we, ch := waiter.NewChannelEntry(nil) 1283 c.WQ.EventRegister(&we, waiter.EventIn) 1284 defer c.WQ.EventUnregister(&we) 1285 1286 if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock { 1287 t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock) 1288 } 1289 1290 data := []byte{1, 2, 3} 1291 c.SendPacket(data, &context.Headers{ 1292 SrcPort: context.TestPort, 1293 DstPort: c.Port, 1294 Flags: header.TCPFlagAck, 1295 SeqNum: 790, 1296 AckNum: c.IRS.Add(1), 1297 RcvWnd: 30000, 1298 }) 1299 1300 // Wait for receive to be notified. 1301 select { 1302 case <-ch: 1303 case <-time.After(3 * time.Second): 1304 t.Fatalf("Timed out waiting for data to arrive") 1305 } 1306 1307 // Check that ACK is received, this happens regardless of the read. 1308 checker.IPv4(t, c.GetPacket(), 1309 checker.TCP( 1310 checker.DstPort(context.TestPort), 1311 checker.SeqNum(uint32(c.IRS)+1), 1312 checker.AckNum(uint32(790+len(data))), 1313 checker.TCPFlags(header.TCPFlagAck), 1314 ), 1315 ) 1316 1317 // Cause a FIN to be generated. 1318 c.EP.Shutdown(tcpip.ShutdownWrite) 1319 1320 // Make sure we get the FIN but DON't ACK IT. 1321 checker.IPv4(t, c.GetPacket(), 1322 checker.TCP( 1323 checker.DstPort(context.TestPort), 1324 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin), 1325 checker.SeqNum(uint32(c.IRS)+1), 1326 )) 1327 1328 if got, want := tcp.EndpointState(c.EP.State()), tcp.StateFinWait1; got != want { 1329 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 1330 } 1331 1332 // Cause a RST to be generated by closing the read end now since we have 1333 // unread data. 1334 c.EP.Shutdown(tcpip.ShutdownRead) 1335 1336 // Make sure we get the RST 1337 checker.IPv4(t, c.GetPacket(), 1338 checker.TCP( 1339 checker.DstPort(context.TestPort), 1340 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagRst), 1341 checker.SeqNum(uint32(c.IRS)+2), 1342 )) 1343 // The RST puts the endpoint into an error state. 1344 if got, want := tcp.EndpointState(c.EP.State()), tcp.StateError; got != want { 1345 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 1346 } 1347 1348 // The ACK to the FIN should now be rejected since the connection has been 1349 // closed by a RST. 1350 c.SendPacket(nil, &context.Headers{ 1351 SrcPort: context.TestPort, 1352 DstPort: c.Port, 1353 Flags: header.TCPFlagAck, 1354 SeqNum: seqnum.Value(790 + len(data)), 1355 AckNum: c.IRS.Add(seqnum.Size(2)), 1356 RcvWnd: 30000, 1357 }) 1358 } 1359 1360 func TestShutdownRead(t *testing.T) { 1361 c := context.New(t, defaultMTU) 1362 defer c.Cleanup() 1363 1364 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 1365 1366 if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock { 1367 t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock) 1368 } 1369 1370 if err := c.EP.Shutdown(tcpip.ShutdownRead); err != nil { 1371 t.Fatalf("Shutdown failed: %v", err) 1372 } 1373 1374 if _, _, err := c.EP.Read(nil); err != tcpip.ErrClosedForReceive { 1375 t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrClosedForReceive) 1376 } 1377 var want uint64 = 1 1378 if got := c.EP.Stats().(*tcp.Stats).ReadErrors.ReadClosed.Value(); got != want { 1379 t.Fatalf("got EP stats Stats.ReadErrors.ReadClosed got %v want %v", got, want) 1380 } 1381 } 1382 1383 func TestFullWindowReceive(t *testing.T) { 1384 c := context.New(t, defaultMTU) 1385 defer c.Cleanup() 1386 1387 c.CreateConnected(789, 30000, 10) 1388 1389 we, ch := waiter.NewChannelEntry(nil) 1390 c.WQ.EventRegister(&we, waiter.EventIn) 1391 defer c.WQ.EventUnregister(&we) 1392 1393 _, _, err := c.EP.Read(nil) 1394 if err != tcpip.ErrWouldBlock { 1395 t.Fatalf("Read failed: %v", err) 1396 } 1397 1398 // Fill up the window. 1399 data := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 1400 c.SendPacket(data, &context.Headers{ 1401 SrcPort: context.TestPort, 1402 DstPort: c.Port, 1403 Flags: header.TCPFlagAck, 1404 SeqNum: 790, 1405 AckNum: c.IRS.Add(1), 1406 RcvWnd: 30000, 1407 }) 1408 1409 // Wait for receive to be notified. 1410 select { 1411 case <-ch: 1412 case <-time.After(5 * time.Second): 1413 t.Fatalf("Timed out waiting for data to arrive") 1414 } 1415 1416 // Check that data is acknowledged, and window goes to zero. 1417 checker.IPv4(t, c.GetPacket(), 1418 checker.TCP( 1419 checker.DstPort(context.TestPort), 1420 checker.SeqNum(uint32(c.IRS)+1), 1421 checker.AckNum(uint32(790+len(data))), 1422 checker.TCPFlags(header.TCPFlagAck), 1423 checker.Window(0), 1424 ), 1425 ) 1426 1427 // Receive data and check it. 1428 v, _, err := c.EP.Read(nil) 1429 if err != nil { 1430 t.Fatalf("Read failed: %v", err) 1431 } 1432 1433 if !bytes.Equal(data, v) { 1434 t.Fatalf("got data = %v, want = %v", v, data) 1435 } 1436 1437 var want uint64 = 1 1438 if got := c.EP.Stats().(*tcp.Stats).ReceiveErrors.ZeroRcvWindowState.Value(); got != want { 1439 t.Fatalf("got EP stats ReceiveErrors.ZeroRcvWindowState got %v want %v", got, want) 1440 } 1441 1442 // Check that we get an ACK for the newly non-zero window. 1443 checker.IPv4(t, c.GetPacket(), 1444 checker.TCP( 1445 checker.DstPort(context.TestPort), 1446 checker.SeqNum(uint32(c.IRS)+1), 1447 checker.AckNum(uint32(790+len(data))), 1448 checker.TCPFlags(header.TCPFlagAck), 1449 checker.Window(10), 1450 ), 1451 ) 1452 } 1453 1454 func TestNoWindowShrinking(t *testing.T) { 1455 c := context.New(t, defaultMTU) 1456 defer c.Cleanup() 1457 1458 // Start off with a window size of 10, then shrink it to 5. 1459 c.CreateConnected(789, 30000, 10) 1460 1461 if err := c.EP.SetSockOptInt(tcpip.ReceiveBufferSizeOption, 5); err != nil { 1462 t.Fatalf("SetSockOpt failed: %v", err) 1463 } 1464 1465 we, ch := waiter.NewChannelEntry(nil) 1466 c.WQ.EventRegister(&we, waiter.EventIn) 1467 defer c.WQ.EventUnregister(&we) 1468 1469 if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock { 1470 t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock) 1471 } 1472 1473 // Send 3 bytes, check that the peer acknowledges them. 1474 data := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 1475 c.SendPacket(data[:3], &context.Headers{ 1476 SrcPort: context.TestPort, 1477 DstPort: c.Port, 1478 Flags: header.TCPFlagAck, 1479 SeqNum: 790, 1480 AckNum: c.IRS.Add(1), 1481 RcvWnd: 30000, 1482 }) 1483 1484 // Wait for receive to be notified. 1485 select { 1486 case <-ch: 1487 case <-time.After(5 * time.Second): 1488 t.Fatalf("Timed out waiting for data to arrive") 1489 } 1490 1491 // Check that data is acknowledged, and that window doesn't go to zero 1492 // just yet because it was previously set to 10. It must go to 7 now. 1493 checker.IPv4(t, c.GetPacket(), 1494 checker.TCP( 1495 checker.DstPort(context.TestPort), 1496 checker.SeqNum(uint32(c.IRS)+1), 1497 checker.AckNum(793), 1498 checker.TCPFlags(header.TCPFlagAck), 1499 checker.Window(7), 1500 ), 1501 ) 1502 1503 // Send 7 more bytes, check that the window fills up. 1504 c.SendPacket(data[3:], &context.Headers{ 1505 SrcPort: context.TestPort, 1506 DstPort: c.Port, 1507 Flags: header.TCPFlagAck, 1508 SeqNum: 793, 1509 AckNum: c.IRS.Add(1), 1510 RcvWnd: 30000, 1511 }) 1512 1513 select { 1514 case <-ch: 1515 case <-time.After(5 * time.Second): 1516 t.Fatalf("Timed out waiting for data to arrive") 1517 } 1518 1519 checker.IPv4(t, c.GetPacket(), 1520 checker.TCP( 1521 checker.DstPort(context.TestPort), 1522 checker.SeqNum(uint32(c.IRS)+1), 1523 checker.AckNum(uint32(790+len(data))), 1524 checker.TCPFlags(header.TCPFlagAck), 1525 checker.Window(0), 1526 ), 1527 ) 1528 1529 // Receive data and check it. 1530 read := make([]byte, 0, 10) 1531 for len(read) < len(data) { 1532 v, _, err := c.EP.Read(nil) 1533 if err != nil { 1534 t.Fatalf("Read failed: %v", err) 1535 } 1536 1537 read = append(read, v...) 1538 } 1539 1540 if !bytes.Equal(data, read) { 1541 t.Fatalf("got data = %v, want = %v", read, data) 1542 } 1543 1544 // Check that we get an ACK for the newly non-zero window, which is the 1545 // new size. 1546 checker.IPv4(t, c.GetPacket(), 1547 checker.TCP( 1548 checker.DstPort(context.TestPort), 1549 checker.SeqNum(uint32(c.IRS)+1), 1550 checker.AckNum(uint32(790+len(data))), 1551 checker.TCPFlags(header.TCPFlagAck), 1552 checker.Window(5), 1553 ), 1554 ) 1555 } 1556 1557 func TestSimpleSend(t *testing.T) { 1558 c := context.New(t, defaultMTU) 1559 defer c.Cleanup() 1560 1561 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 1562 1563 data := []byte{1, 2, 3} 1564 view := buffer.NewView(len(data)) 1565 copy(view, data) 1566 1567 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 1568 t.Fatalf("Write failed: %v", err) 1569 } 1570 1571 // Check that data is received. 1572 b := c.GetPacket() 1573 checker.IPv4(t, b, 1574 checker.PayloadLen(len(data)+header.TCPMinimumSize), 1575 checker.TCP( 1576 checker.DstPort(context.TestPort), 1577 checker.SeqNum(uint32(c.IRS)+1), 1578 checker.AckNum(790), 1579 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 1580 ), 1581 ) 1582 1583 if p := b[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(data, p) { 1584 t.Fatalf("got data = %v, want = %v", p, data) 1585 } 1586 1587 // Acknowledge the data. 1588 c.SendPacket(nil, &context.Headers{ 1589 SrcPort: context.TestPort, 1590 DstPort: c.Port, 1591 Flags: header.TCPFlagAck, 1592 SeqNum: 790, 1593 AckNum: c.IRS.Add(1 + seqnum.Size(len(data))), 1594 RcvWnd: 30000, 1595 }) 1596 } 1597 1598 func TestZeroWindowSend(t *testing.T) { 1599 c := context.New(t, defaultMTU) 1600 defer c.Cleanup() 1601 1602 c.CreateConnected(789, 0, -1 /* epRcvBuf */) 1603 1604 data := []byte{1, 2, 3} 1605 view := buffer.NewView(len(data)) 1606 copy(view, data) 1607 1608 _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}) 1609 if err != nil { 1610 t.Fatalf("Write failed: %v", err) 1611 } 1612 1613 // Since the window is currently zero, check that no packet is received. 1614 c.CheckNoPacket("Packet received when window is zero") 1615 1616 // Open up the window. Data should be received now. 1617 c.SendPacket(nil, &context.Headers{ 1618 SrcPort: context.TestPort, 1619 DstPort: c.Port, 1620 Flags: header.TCPFlagAck, 1621 SeqNum: 790, 1622 AckNum: c.IRS.Add(1), 1623 RcvWnd: 30000, 1624 }) 1625 1626 // Check that data is received. 1627 b := c.GetPacket() 1628 checker.IPv4(t, b, 1629 checker.PayloadLen(len(data)+header.TCPMinimumSize), 1630 checker.TCP( 1631 checker.DstPort(context.TestPort), 1632 checker.SeqNum(uint32(c.IRS)+1), 1633 checker.AckNum(790), 1634 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 1635 ), 1636 ) 1637 1638 if p := b[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(data, p) { 1639 t.Fatalf("got data = %v, want = %v", p, data) 1640 } 1641 1642 // Acknowledge the data. 1643 c.SendPacket(nil, &context.Headers{ 1644 SrcPort: context.TestPort, 1645 DstPort: c.Port, 1646 Flags: header.TCPFlagAck, 1647 SeqNum: 790, 1648 AckNum: c.IRS.Add(1 + seqnum.Size(len(data))), 1649 RcvWnd: 30000, 1650 }) 1651 } 1652 1653 func TestScaledWindowConnect(t *testing.T) { 1654 // This test ensures that window scaling is used when the peer 1655 // does advertise it and connection is established with Connect(). 1656 c := context.New(t, defaultMTU) 1657 defer c.Cleanup() 1658 1659 // Set the window size greater than the maximum non-scaled window. 1660 c.CreateConnectedWithRawOptions(789, 30000, 65535*3, []byte{ 1661 header.TCPOptionWS, 3, 0, header.TCPOptionNOP, 1662 }) 1663 1664 data := []byte{1, 2, 3} 1665 view := buffer.NewView(len(data)) 1666 copy(view, data) 1667 1668 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 1669 t.Fatalf("Write failed: %v", err) 1670 } 1671 1672 // Check that data is received, and that advertised window is 0xbfff, 1673 // that is, that it is scaled. 1674 b := c.GetPacket() 1675 checker.IPv4(t, b, 1676 checker.PayloadLen(len(data)+header.TCPMinimumSize), 1677 checker.TCP( 1678 checker.DstPort(context.TestPort), 1679 checker.SeqNum(uint32(c.IRS)+1), 1680 checker.AckNum(790), 1681 checker.Window(0xbfff), 1682 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 1683 ), 1684 ) 1685 } 1686 1687 func TestNonScaledWindowConnect(t *testing.T) { 1688 // This test ensures that window scaling is not used when the peer 1689 // doesn't advertise it and connection is established with Connect(). 1690 c := context.New(t, defaultMTU) 1691 defer c.Cleanup() 1692 1693 // Set the window size greater than the maximum non-scaled window. 1694 c.CreateConnected(789, 30000, 65535*3) 1695 1696 data := []byte{1, 2, 3} 1697 view := buffer.NewView(len(data)) 1698 copy(view, data) 1699 1700 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 1701 t.Fatalf("Write failed: %v", err) 1702 } 1703 1704 // Check that data is received, and that advertised window is 0xffff, 1705 // that is, that it's not scaled. 1706 b := c.GetPacket() 1707 checker.IPv4(t, b, 1708 checker.PayloadLen(len(data)+header.TCPMinimumSize), 1709 checker.TCP( 1710 checker.DstPort(context.TestPort), 1711 checker.SeqNum(uint32(c.IRS)+1), 1712 checker.AckNum(790), 1713 checker.Window(0xffff), 1714 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 1715 ), 1716 ) 1717 } 1718 1719 func TestScaledWindowAccept(t *testing.T) { 1720 // This test ensures that window scaling is used when the peer 1721 // does advertise it and connection is established with Accept(). 1722 c := context.New(t, defaultMTU) 1723 defer c.Cleanup() 1724 1725 // Create EP and start listening. 1726 wq := &waiter.Queue{} 1727 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq) 1728 if err != nil { 1729 t.Fatalf("NewEndpoint failed: %v", err) 1730 } 1731 defer ep.Close() 1732 1733 // Set the window size greater than the maximum non-scaled window. 1734 if err := ep.SetSockOptInt(tcpip.ReceiveBufferSizeOption, 65535*3); err != nil { 1735 t.Fatalf("SetSockOpt failed failed: %v", err) 1736 } 1737 1738 if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 1739 t.Fatalf("Bind failed: %v", err) 1740 } 1741 1742 if err := ep.Listen(10); err != nil { 1743 t.Fatalf("Listen failed: %v", err) 1744 } 1745 1746 // Do 3-way handshake. 1747 c.PassiveConnectWithOptions(100, 2, header.TCPSynOptions{MSS: defaultIPv4MSS}) 1748 1749 // Try to accept the connection. 1750 we, ch := waiter.NewChannelEntry(nil) 1751 wq.EventRegister(&we, waiter.EventIn) 1752 defer wq.EventUnregister(&we) 1753 1754 c.EP, _, err = ep.Accept() 1755 if err == tcpip.ErrWouldBlock { 1756 // Wait for connection to be established. 1757 select { 1758 case <-ch: 1759 c.EP, _, err = ep.Accept() 1760 if err != nil { 1761 t.Fatalf("Accept failed: %v", err) 1762 } 1763 1764 case <-time.After(1 * time.Second): 1765 t.Fatalf("Timed out waiting for accept") 1766 } 1767 } 1768 1769 data := []byte{1, 2, 3} 1770 view := buffer.NewView(len(data)) 1771 copy(view, data) 1772 1773 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 1774 t.Fatalf("Write failed: %v", err) 1775 } 1776 1777 // Check that data is received, and that advertised window is 0xbfff, 1778 // that is, that it is scaled. 1779 b := c.GetPacket() 1780 checker.IPv4(t, b, 1781 checker.PayloadLen(len(data)+header.TCPMinimumSize), 1782 checker.TCP( 1783 checker.DstPort(context.TestPort), 1784 checker.SeqNum(uint32(c.IRS)+1), 1785 checker.AckNum(790), 1786 checker.Window(0xbfff), 1787 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 1788 ), 1789 ) 1790 } 1791 1792 func TestNonScaledWindowAccept(t *testing.T) { 1793 // This test ensures that window scaling is not used when the peer 1794 // doesn't advertise it and connection is established with Accept(). 1795 c := context.New(t, defaultMTU) 1796 defer c.Cleanup() 1797 1798 // Create EP and start listening. 1799 wq := &waiter.Queue{} 1800 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq) 1801 if err != nil { 1802 t.Fatalf("NewEndpoint failed: %v", err) 1803 } 1804 defer ep.Close() 1805 1806 // Set the window size greater than the maximum non-scaled window. 1807 if err := ep.SetSockOptInt(tcpip.ReceiveBufferSizeOption, 65535*3); err != nil { 1808 t.Fatalf("SetSockOpt failed failed: %v", err) 1809 } 1810 1811 if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 1812 t.Fatalf("Bind failed: %v", err) 1813 } 1814 1815 if err := ep.Listen(10); err != nil { 1816 t.Fatalf("Listen failed: %v", err) 1817 } 1818 1819 // Do 3-way handshake w/ window scaling disabled. The SYN-ACK to the SYN 1820 // should not carry the window scaling option. 1821 c.PassiveConnect(100, -1, header.TCPSynOptions{MSS: defaultIPv4MSS}) 1822 1823 // Try to accept the connection. 1824 we, ch := waiter.NewChannelEntry(nil) 1825 wq.EventRegister(&we, waiter.EventIn) 1826 defer wq.EventUnregister(&we) 1827 1828 c.EP, _, err = ep.Accept() 1829 if err == tcpip.ErrWouldBlock { 1830 // Wait for connection to be established. 1831 select { 1832 case <-ch: 1833 c.EP, _, err = ep.Accept() 1834 if err != nil { 1835 t.Fatalf("Accept failed: %v", err) 1836 } 1837 1838 case <-time.After(1 * time.Second): 1839 t.Fatalf("Timed out waiting for accept") 1840 } 1841 } 1842 1843 data := []byte{1, 2, 3} 1844 view := buffer.NewView(len(data)) 1845 copy(view, data) 1846 1847 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 1848 t.Fatalf("Write failed: %v", err) 1849 } 1850 1851 // Check that data is received, and that advertised window is 0xffff, 1852 // that is, that it's not scaled. 1853 b := c.GetPacket() 1854 checker.IPv4(t, b, 1855 checker.PayloadLen(len(data)+header.TCPMinimumSize), 1856 checker.TCP( 1857 checker.DstPort(context.TestPort), 1858 checker.SeqNum(uint32(c.IRS)+1), 1859 checker.AckNum(790), 1860 checker.Window(0xffff), 1861 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 1862 ), 1863 ) 1864 } 1865 1866 func TestZeroScaledWindowReceive(t *testing.T) { 1867 // This test ensures that the endpoint sends a non-zero window size 1868 // advertisement when the scaled window transitions from 0 to non-zero, 1869 // but the actual window (not scaled) hasn't gotten to zero. 1870 c := context.New(t, defaultMTU) 1871 defer c.Cleanup() 1872 1873 // Set the window size such that a window scale of 4 will be used. 1874 const wnd = 65535 * 10 1875 const ws = uint32(4) 1876 c.CreateConnectedWithRawOptions(789, 30000, wnd, []byte{ 1877 header.TCPOptionWS, 3, 0, header.TCPOptionNOP, 1878 }) 1879 1880 // Write chunks of 50000 bytes. 1881 remain := wnd 1882 sent := 0 1883 data := make([]byte, 50000) 1884 for remain > len(data) { 1885 c.SendPacket(data, &context.Headers{ 1886 SrcPort: context.TestPort, 1887 DstPort: c.Port, 1888 Flags: header.TCPFlagAck, 1889 SeqNum: seqnum.Value(790 + sent), 1890 AckNum: c.IRS.Add(1), 1891 RcvWnd: 30000, 1892 }) 1893 sent += len(data) 1894 remain -= len(data) 1895 checker.IPv4(t, c.GetPacket(), 1896 checker.PayloadLen(header.TCPMinimumSize), 1897 checker.TCP( 1898 checker.DstPort(context.TestPort), 1899 checker.SeqNum(uint32(c.IRS)+1), 1900 checker.AckNum(uint32(790+sent)), 1901 checker.Window(uint16(remain>>ws)), 1902 checker.TCPFlags(header.TCPFlagAck), 1903 ), 1904 ) 1905 } 1906 1907 // Make the window non-zero, but the scaled window zero. 1908 if remain >= 16 { 1909 data = data[:remain-15] 1910 c.SendPacket(data, &context.Headers{ 1911 SrcPort: context.TestPort, 1912 DstPort: c.Port, 1913 Flags: header.TCPFlagAck, 1914 SeqNum: seqnum.Value(790 + sent), 1915 AckNum: c.IRS.Add(1), 1916 RcvWnd: 30000, 1917 }) 1918 sent += len(data) 1919 remain -= len(data) 1920 checker.IPv4(t, c.GetPacket(), 1921 checker.PayloadLen(header.TCPMinimumSize), 1922 checker.TCP( 1923 checker.DstPort(context.TestPort), 1924 checker.SeqNum(uint32(c.IRS)+1), 1925 checker.AckNum(uint32(790+sent)), 1926 checker.Window(0), 1927 checker.TCPFlags(header.TCPFlagAck), 1928 ), 1929 ) 1930 } 1931 1932 // Read some data. An ack should be sent in response to that. 1933 v, _, err := c.EP.Read(nil) 1934 if err != nil { 1935 t.Fatalf("Read failed: %v", err) 1936 } 1937 1938 checker.IPv4(t, c.GetPacket(), 1939 checker.PayloadLen(header.TCPMinimumSize), 1940 checker.TCP( 1941 checker.DstPort(context.TestPort), 1942 checker.SeqNum(uint32(c.IRS)+1), 1943 checker.AckNum(uint32(790+sent)), 1944 checker.Window(uint16(len(v)>>ws)), 1945 checker.TCPFlags(header.TCPFlagAck), 1946 ), 1947 ) 1948 } 1949 1950 func TestSegmentMerging(t *testing.T) { 1951 tests := []struct { 1952 name string 1953 stop func(tcpip.Endpoint) 1954 resume func(tcpip.Endpoint) 1955 }{ 1956 { 1957 "stop work", 1958 func(ep tcpip.Endpoint) { 1959 ep.(interface{ StopWork() }).StopWork() 1960 }, 1961 func(ep tcpip.Endpoint) { 1962 ep.(interface{ ResumeWork() }).ResumeWork() 1963 }, 1964 }, 1965 { 1966 "cork", 1967 func(ep tcpip.Endpoint) { 1968 ep.SetSockOpt(tcpip.CorkOption(1)) 1969 }, 1970 func(ep tcpip.Endpoint) { 1971 ep.SetSockOpt(tcpip.CorkOption(0)) 1972 }, 1973 }, 1974 } 1975 1976 for _, test := range tests { 1977 t.Run(test.name, func(t *testing.T) { 1978 c := context.New(t, defaultMTU) 1979 defer c.Cleanup() 1980 1981 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 1982 1983 // Prevent the endpoint from processing packets. 1984 test.stop(c.EP) 1985 1986 var allData []byte 1987 for i, data := range [][]byte{{1, 2, 3, 4}, {5, 6, 7}, {8, 9}, {10}, {11}} { 1988 allData = append(allData, data...) 1989 view := buffer.NewViewFromBytes(data) 1990 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 1991 t.Fatalf("Write #%d failed: %v", i+1, err) 1992 } 1993 } 1994 1995 // Let the endpoint process the segments that we just sent. 1996 test.resume(c.EP) 1997 1998 // Check that data is received. 1999 b := c.GetPacket() 2000 checker.IPv4(t, b, 2001 checker.PayloadLen(len(allData)+header.TCPMinimumSize), 2002 checker.TCP( 2003 checker.DstPort(context.TestPort), 2004 checker.SeqNum(uint32(c.IRS)+1), 2005 checker.AckNum(790), 2006 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 2007 ), 2008 ) 2009 2010 if got := b[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(got, allData) { 2011 t.Fatalf("got data = %v, want = %v", got, allData) 2012 } 2013 2014 // Acknowledge the data. 2015 c.SendPacket(nil, &context.Headers{ 2016 SrcPort: context.TestPort, 2017 DstPort: c.Port, 2018 Flags: header.TCPFlagAck, 2019 SeqNum: 790, 2020 AckNum: c.IRS.Add(1 + seqnum.Size(len(allData))), 2021 RcvWnd: 30000, 2022 }) 2023 }) 2024 } 2025 } 2026 2027 func TestDelay(t *testing.T) { 2028 c := context.New(t, defaultMTU) 2029 defer c.Cleanup() 2030 2031 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 2032 2033 c.EP.SetSockOptInt(tcpip.DelayOption, 1) 2034 2035 var allData []byte 2036 for i, data := range [][]byte{{0}, {1, 2, 3, 4}, {5, 6, 7}, {8, 9}, {10}, {11}} { 2037 allData = append(allData, data...) 2038 view := buffer.NewViewFromBytes(data) 2039 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 2040 t.Fatalf("Write #%d failed: %v", i+1, err) 2041 } 2042 } 2043 2044 seq := c.IRS.Add(1) 2045 for _, want := range [][]byte{allData[:1], allData[1:]} { 2046 // Check that data is received. 2047 b := c.GetPacket() 2048 checker.IPv4(t, b, 2049 checker.PayloadLen(len(want)+header.TCPMinimumSize), 2050 checker.TCP( 2051 checker.DstPort(context.TestPort), 2052 checker.SeqNum(uint32(seq)), 2053 checker.AckNum(790), 2054 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 2055 ), 2056 ) 2057 2058 if got := b[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(got, want) { 2059 t.Fatalf("got data = %v, want = %v", got, want) 2060 } 2061 2062 seq = seq.Add(seqnum.Size(len(want))) 2063 // Acknowledge the data. 2064 c.SendPacket(nil, &context.Headers{ 2065 SrcPort: context.TestPort, 2066 DstPort: c.Port, 2067 Flags: header.TCPFlagAck, 2068 SeqNum: 790, 2069 AckNum: seq, 2070 RcvWnd: 30000, 2071 }) 2072 } 2073 } 2074 2075 func TestUndelay(t *testing.T) { 2076 c := context.New(t, defaultMTU) 2077 defer c.Cleanup() 2078 2079 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 2080 2081 c.EP.SetSockOptInt(tcpip.DelayOption, 1) 2082 2083 allData := [][]byte{{0}, {1, 2, 3}} 2084 for i, data := range allData { 2085 view := buffer.NewViewFromBytes(data) 2086 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 2087 t.Fatalf("Write #%d failed: %v", i+1, err) 2088 } 2089 } 2090 2091 seq := c.IRS.Add(1) 2092 2093 // Check that data is received. 2094 first := c.GetPacket() 2095 checker.IPv4(t, first, 2096 checker.PayloadLen(len(allData[0])+header.TCPMinimumSize), 2097 checker.TCP( 2098 checker.DstPort(context.TestPort), 2099 checker.SeqNum(uint32(seq)), 2100 checker.AckNum(790), 2101 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 2102 ), 2103 ) 2104 2105 if got, want := first[header.IPv4MinimumSize+header.TCPMinimumSize:], allData[0]; !bytes.Equal(got, want) { 2106 t.Fatalf("got first packet's data = %v, want = %v", got, want) 2107 } 2108 2109 seq = seq.Add(seqnum.Size(len(allData[0]))) 2110 2111 // Check that we don't get the second packet yet. 2112 c.CheckNoPacketTimeout("delayed second packet transmitted", 100*time.Millisecond) 2113 2114 c.EP.SetSockOptInt(tcpip.DelayOption, 0) 2115 2116 // Check that data is received. 2117 second := c.GetPacket() 2118 checker.IPv4(t, second, 2119 checker.PayloadLen(len(allData[1])+header.TCPMinimumSize), 2120 checker.TCP( 2121 checker.DstPort(context.TestPort), 2122 checker.SeqNum(uint32(seq)), 2123 checker.AckNum(790), 2124 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 2125 ), 2126 ) 2127 2128 if got, want := second[header.IPv4MinimumSize+header.TCPMinimumSize:], allData[1]; !bytes.Equal(got, want) { 2129 t.Fatalf("got second packet's data = %v, want = %v", got, want) 2130 } 2131 2132 seq = seq.Add(seqnum.Size(len(allData[1]))) 2133 2134 // Acknowledge the data. 2135 c.SendPacket(nil, &context.Headers{ 2136 SrcPort: context.TestPort, 2137 DstPort: c.Port, 2138 Flags: header.TCPFlagAck, 2139 SeqNum: 790, 2140 AckNum: seq, 2141 RcvWnd: 30000, 2142 }) 2143 } 2144 2145 func TestMSSNotDelayed(t *testing.T) { 2146 tests := []struct { 2147 name string 2148 fn func(tcpip.Endpoint) 2149 }{ 2150 {"no-op", func(tcpip.Endpoint) {}}, 2151 {"delay", func(ep tcpip.Endpoint) { ep.SetSockOptInt(tcpip.DelayOption, 1) }}, 2152 {"cork", func(ep tcpip.Endpoint) { ep.SetSockOpt(tcpip.CorkOption(1)) }}, 2153 } 2154 2155 for _, test := range tests { 2156 t.Run(test.name, func(t *testing.T) { 2157 const maxPayload = 100 2158 c := context.New(t, defaultMTU) 2159 defer c.Cleanup() 2160 2161 c.CreateConnectedWithRawOptions(789, 30000, -1 /* epRcvBuf */, []byte{ 2162 header.TCPOptionMSS, 4, byte(maxPayload / 256), byte(maxPayload % 256), 2163 }) 2164 2165 test.fn(c.EP) 2166 2167 allData := [][]byte{{0}, make([]byte, maxPayload), make([]byte, maxPayload)} 2168 for i, data := range allData { 2169 view := buffer.NewViewFromBytes(data) 2170 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 2171 t.Fatalf("Write #%d failed: %v", i+1, err) 2172 } 2173 } 2174 2175 seq := c.IRS.Add(1) 2176 2177 for i, data := range allData { 2178 // Check that data is received. 2179 packet := c.GetPacket() 2180 checker.IPv4(t, packet, 2181 checker.PayloadLen(len(data)+header.TCPMinimumSize), 2182 checker.TCP( 2183 checker.DstPort(context.TestPort), 2184 checker.SeqNum(uint32(seq)), 2185 checker.AckNum(790), 2186 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 2187 ), 2188 ) 2189 2190 if got, want := packet[header.IPv4MinimumSize+header.TCPMinimumSize:], data; !bytes.Equal(got, want) { 2191 t.Fatalf("got packet #%d's data = %v, want = %v", i+1, got, want) 2192 } 2193 2194 seq = seq.Add(seqnum.Size(len(data))) 2195 } 2196 2197 // Acknowledge the data. 2198 c.SendPacket(nil, &context.Headers{ 2199 SrcPort: context.TestPort, 2200 DstPort: c.Port, 2201 Flags: header.TCPFlagAck, 2202 SeqNum: 790, 2203 AckNum: seq, 2204 RcvWnd: 30000, 2205 }) 2206 }) 2207 } 2208 } 2209 2210 func testBrokenUpWrite(t *testing.T, c *context.Context, maxPayload int) { 2211 payloadMultiplier := 10 2212 dataLen := payloadMultiplier * maxPayload 2213 data := make([]byte, dataLen) 2214 for i := range data { 2215 data[i] = byte(i) 2216 } 2217 2218 view := buffer.NewView(len(data)) 2219 copy(view, data) 2220 2221 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 2222 t.Fatalf("Write failed: %v", err) 2223 } 2224 2225 // Check that data is received in chunks. 2226 bytesReceived := 0 2227 numPackets := 0 2228 for bytesReceived != dataLen { 2229 b := c.GetPacket() 2230 numPackets++ 2231 tcpHdr := header.TCP(header.IPv4(b).Payload()) 2232 payloadLen := len(tcpHdr.Payload()) 2233 checker.IPv4(t, b, 2234 checker.TCP( 2235 checker.DstPort(context.TestPort), 2236 checker.SeqNum(uint32(c.IRS)+1+uint32(bytesReceived)), 2237 checker.AckNum(790), 2238 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 2239 ), 2240 ) 2241 2242 pdata := data[bytesReceived : bytesReceived+payloadLen] 2243 if p := tcpHdr.Payload(); !bytes.Equal(pdata, p) { 2244 t.Fatalf("got data = %v, want = %v", p, pdata) 2245 } 2246 bytesReceived += payloadLen 2247 var options []byte 2248 if c.TimeStampEnabled { 2249 // If timestamp option is enabled, echo back the timestamp and increment 2250 // the TSEcr value included in the packet and send that back as the TSVal. 2251 parsedOpts := tcpHdr.ParsedOptions() 2252 tsOpt := [12]byte{header.TCPOptionNOP, header.TCPOptionNOP} 2253 header.EncodeTSOption(parsedOpts.TSEcr+1, parsedOpts.TSVal, tsOpt[2:]) 2254 options = tsOpt[:] 2255 } 2256 // Acknowledge the data. 2257 c.SendPacket(nil, &context.Headers{ 2258 SrcPort: context.TestPort, 2259 DstPort: c.Port, 2260 Flags: header.TCPFlagAck, 2261 SeqNum: 790, 2262 AckNum: c.IRS.Add(1 + seqnum.Size(bytesReceived)), 2263 RcvWnd: 30000, 2264 TCPOpts: options, 2265 }) 2266 } 2267 if numPackets == 1 { 2268 t.Fatalf("expected write to be broken up into multiple packets, but got 1 packet") 2269 } 2270 } 2271 2272 func TestSendGreaterThanMTU(t *testing.T) { 2273 const maxPayload = 100 2274 c := context.New(t, uint32(header.TCPMinimumSize+header.IPv4MinimumSize+maxPayload)) 2275 defer c.Cleanup() 2276 2277 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 2278 testBrokenUpWrite(t, c, maxPayload) 2279 } 2280 2281 func TestSetTTL(t *testing.T) { 2282 for _, wantTTL := range []uint8{1, 2, 50, 64, 128, 254, 255} { 2283 t.Run(fmt.Sprintf("TTL:%d", wantTTL), func(t *testing.T) { 2284 c := context.New(t, 65535) 2285 defer c.Cleanup() 2286 2287 var err *tcpip.Error 2288 c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{}) 2289 if err != nil { 2290 t.Fatalf("NewEndpoint failed: %v", err) 2291 } 2292 2293 if err := c.EP.SetSockOpt(tcpip.TTLOption(wantTTL)); err != nil { 2294 t.Fatalf("SetSockOpt failed: %v", err) 2295 } 2296 2297 if err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrConnectStarted { 2298 t.Fatalf("Unexpected return value from Connect: %v", err) 2299 } 2300 2301 // Receive SYN packet. 2302 b := c.GetPacket() 2303 2304 checker.IPv4(t, b, checker.TTL(wantTTL)) 2305 }) 2306 } 2307 } 2308 2309 func TestActiveSendMSSLessThanMTU(t *testing.T) { 2310 const maxPayload = 100 2311 c := context.New(t, 65535) 2312 defer c.Cleanup() 2313 2314 c.CreateConnectedWithRawOptions(789, 30000, -1 /* epRcvBuf */, []byte{ 2315 header.TCPOptionMSS, 4, byte(maxPayload / 256), byte(maxPayload % 256), 2316 }) 2317 testBrokenUpWrite(t, c, maxPayload) 2318 } 2319 2320 func TestPassiveSendMSSLessThanMTU(t *testing.T) { 2321 const maxPayload = 100 2322 const mtu = 1200 2323 c := context.New(t, mtu) 2324 defer c.Cleanup() 2325 2326 // Create EP and start listening. 2327 wq := &waiter.Queue{} 2328 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq) 2329 if err != nil { 2330 t.Fatalf("NewEndpoint failed: %v", err) 2331 } 2332 defer ep.Close() 2333 2334 // Set the buffer size to a deterministic size so that we can check the 2335 // window scaling option. 2336 const rcvBufferSize = 0x20000 2337 if err := ep.SetSockOptInt(tcpip.ReceiveBufferSizeOption, rcvBufferSize); err != nil { 2338 t.Fatalf("SetSockOpt failed failed: %v", err) 2339 } 2340 2341 if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 2342 t.Fatalf("Bind failed: %v", err) 2343 } 2344 2345 if err := ep.Listen(10); err != nil { 2346 t.Fatalf("Listen failed: %v", err) 2347 } 2348 2349 // Do 3-way handshake. 2350 c.PassiveConnect(maxPayload, -1, header.TCPSynOptions{MSS: mtu - header.IPv4MinimumSize - header.TCPMinimumSize}) 2351 2352 // Try to accept the connection. 2353 we, ch := waiter.NewChannelEntry(nil) 2354 wq.EventRegister(&we, waiter.EventIn) 2355 defer wq.EventUnregister(&we) 2356 2357 c.EP, _, err = ep.Accept() 2358 if err == tcpip.ErrWouldBlock { 2359 // Wait for connection to be established. 2360 select { 2361 case <-ch: 2362 c.EP, _, err = ep.Accept() 2363 if err != nil { 2364 t.Fatalf("Accept failed: %v", err) 2365 } 2366 2367 case <-time.After(1 * time.Second): 2368 t.Fatalf("Timed out waiting for accept") 2369 } 2370 } 2371 2372 // Check that data gets properly segmented. 2373 testBrokenUpWrite(t, c, maxPayload) 2374 } 2375 2376 func TestSynCookiePassiveSendMSSLessThanMTU(t *testing.T) { 2377 const maxPayload = 536 2378 const mtu = 2000 2379 c := context.New(t, mtu) 2380 defer c.Cleanup() 2381 2382 // Set the SynRcvd threshold to zero to force a syn cookie based accept 2383 // to happen. 2384 saved := tcp.SynRcvdCountThreshold 2385 defer func() { 2386 tcp.SynRcvdCountThreshold = saved 2387 }() 2388 tcp.SynRcvdCountThreshold = 0 2389 2390 // Create EP and start listening. 2391 wq := &waiter.Queue{} 2392 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq) 2393 if err != nil { 2394 t.Fatalf("NewEndpoint failed: %v", err) 2395 } 2396 defer ep.Close() 2397 2398 if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 2399 t.Fatalf("Bind failed: %v", err) 2400 } 2401 2402 if err := ep.Listen(10); err != nil { 2403 t.Fatalf("Listen failed: %v", err) 2404 } 2405 2406 // Do 3-way handshake. 2407 c.PassiveConnect(maxPayload, -1, header.TCPSynOptions{MSS: mtu - header.IPv4MinimumSize - header.TCPMinimumSize}) 2408 2409 // Try to accept the connection. 2410 we, ch := waiter.NewChannelEntry(nil) 2411 wq.EventRegister(&we, waiter.EventIn) 2412 defer wq.EventUnregister(&we) 2413 2414 c.EP, _, err = ep.Accept() 2415 if err == tcpip.ErrWouldBlock { 2416 // Wait for connection to be established. 2417 select { 2418 case <-ch: 2419 c.EP, _, err = ep.Accept() 2420 if err != nil { 2421 t.Fatalf("Accept failed: %v", err) 2422 } 2423 2424 case <-time.After(1 * time.Second): 2425 t.Fatalf("Timed out waiting for accept") 2426 } 2427 } 2428 2429 // Check that data gets properly segmented. 2430 testBrokenUpWrite(t, c, maxPayload) 2431 } 2432 2433 func TestForwarderSendMSSLessThanMTU(t *testing.T) { 2434 const maxPayload = 100 2435 const mtu = 1200 2436 c := context.New(t, mtu) 2437 defer c.Cleanup() 2438 2439 s := c.Stack() 2440 ch := make(chan *tcpip.Error, 1) 2441 f := tcp.NewForwarder(s, 65536, 10, func(r *tcp.ForwarderRequest) { 2442 var err *tcpip.Error 2443 c.EP, err = r.CreateEndpoint(&c.WQ) 2444 ch <- err 2445 }) 2446 s.SetTransportProtocolHandler(tcp.ProtocolNumber, f.HandlePacket) 2447 2448 // Do 3-way handshake. 2449 c.PassiveConnect(maxPayload, -1, header.TCPSynOptions{MSS: mtu - header.IPv4MinimumSize - header.TCPMinimumSize}) 2450 2451 // Wait for connection to be available. 2452 select { 2453 case err := <-ch: 2454 if err != nil { 2455 t.Fatalf("Error creating endpoint: %v", err) 2456 } 2457 case <-time.After(2 * time.Second): 2458 t.Fatalf("Timed out waiting for connection") 2459 } 2460 2461 // Check that data gets properly segmented. 2462 testBrokenUpWrite(t, c, maxPayload) 2463 } 2464 2465 func TestSynOptionsOnActiveConnect(t *testing.T) { 2466 const mtu = 1400 2467 c := context.New(t, mtu) 2468 defer c.Cleanup() 2469 2470 // Create TCP endpoint. 2471 var err *tcpip.Error 2472 c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ) 2473 if err != nil { 2474 t.Fatalf("NewEndpoint failed: %v", err) 2475 } 2476 2477 // Set the buffer size to a deterministic size so that we can check the 2478 // window scaling option. 2479 const rcvBufferSize = 0x20000 2480 const wndScale = 2 2481 if err := c.EP.SetSockOptInt(tcpip.ReceiveBufferSizeOption, rcvBufferSize); err != nil { 2482 t.Fatalf("SetSockOpt failed failed: %v", err) 2483 } 2484 2485 // Start connection attempt. 2486 we, ch := waiter.NewChannelEntry(nil) 2487 c.WQ.EventRegister(&we, waiter.EventOut) 2488 defer c.WQ.EventUnregister(&we) 2489 2490 if err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrConnectStarted { 2491 t.Fatalf("got c.EP.Connect(...) = %v, want = %v", err, tcpip.ErrConnectStarted) 2492 } 2493 2494 // Receive SYN packet. 2495 b := c.GetPacket() 2496 mss := uint16(mtu - header.IPv4MinimumSize - header.TCPMinimumSize) 2497 checker.IPv4(t, b, 2498 checker.TCP( 2499 checker.DstPort(context.TestPort), 2500 checker.TCPFlags(header.TCPFlagSyn), 2501 checker.TCPSynOptions(header.TCPSynOptions{MSS: mss, WS: wndScale}), 2502 ), 2503 ) 2504 2505 tcpHdr := header.TCP(header.IPv4(b).Payload()) 2506 c.IRS = seqnum.Value(tcpHdr.SequenceNumber()) 2507 2508 // Wait for retransmit. 2509 time.Sleep(1 * time.Second) 2510 checker.IPv4(t, c.GetPacket(), 2511 checker.TCP( 2512 checker.DstPort(context.TestPort), 2513 checker.TCPFlags(header.TCPFlagSyn), 2514 checker.SrcPort(tcpHdr.SourcePort()), 2515 checker.SeqNum(tcpHdr.SequenceNumber()), 2516 checker.TCPSynOptions(header.TCPSynOptions{MSS: mss, WS: wndScale}), 2517 ), 2518 ) 2519 2520 // Send SYN-ACK. 2521 iss := seqnum.Value(789) 2522 c.SendPacket(nil, &context.Headers{ 2523 SrcPort: tcpHdr.DestinationPort(), 2524 DstPort: tcpHdr.SourcePort(), 2525 Flags: header.TCPFlagSyn | header.TCPFlagAck, 2526 SeqNum: iss, 2527 AckNum: c.IRS.Add(1), 2528 RcvWnd: 30000, 2529 }) 2530 2531 // Receive ACK packet. 2532 checker.IPv4(t, c.GetPacket(), 2533 checker.TCP( 2534 checker.DstPort(context.TestPort), 2535 checker.TCPFlags(header.TCPFlagAck), 2536 checker.SeqNum(uint32(c.IRS)+1), 2537 checker.AckNum(uint32(iss)+1), 2538 ), 2539 ) 2540 2541 // Wait for connection to be established. 2542 select { 2543 case <-ch: 2544 if err := c.EP.GetSockOpt(tcpip.ErrorOption{}); err != nil { 2545 t.Fatalf("GetSockOpt failed: %v", err) 2546 } 2547 case <-time.After(1 * time.Second): 2548 t.Fatalf("Timed out waiting for connection") 2549 } 2550 } 2551 2552 func TestCloseListener(t *testing.T) { 2553 c := context.New(t, defaultMTU) 2554 defer c.Cleanup() 2555 2556 // Create listener. 2557 var wq waiter.Queue 2558 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq) 2559 if err != nil { 2560 t.Fatalf("NewEndpoint failed: %v", err) 2561 } 2562 2563 if err := ep.Bind(tcpip.FullAddress{}); err != nil { 2564 t.Fatalf("Bind failed: %v", err) 2565 } 2566 2567 if err := ep.Listen(10); err != nil { 2568 t.Fatalf("Listen failed: %v", err) 2569 } 2570 2571 // Close the listener and measure how long it takes. 2572 t0 := time.Now() 2573 ep.Close() 2574 if diff := time.Now().Sub(t0); diff > 3*time.Second { 2575 t.Fatalf("Took too long to close: %v", diff) 2576 } 2577 } 2578 2579 func TestReceiveOnResetConnection(t *testing.T) { 2580 c := context.New(t, defaultMTU) 2581 defer c.Cleanup() 2582 2583 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 2584 2585 // Send RST segment. 2586 c.SendPacket(nil, &context.Headers{ 2587 SrcPort: context.TestPort, 2588 DstPort: c.Port, 2589 Flags: header.TCPFlagRst, 2590 SeqNum: 790, 2591 RcvWnd: 30000, 2592 }) 2593 2594 // Try to read. 2595 we, ch := waiter.NewChannelEntry(nil) 2596 c.WQ.EventRegister(&we, waiter.EventIn) 2597 defer c.WQ.EventUnregister(&we) 2598 2599 loop: 2600 for { 2601 switch _, _, err := c.EP.Read(nil); err { 2602 case tcpip.ErrWouldBlock: 2603 select { 2604 case <-ch: 2605 case <-time.After(1 * time.Second): 2606 t.Fatalf("Timed out waiting for reset to arrive") 2607 } 2608 case tcpip.ErrConnectionReset: 2609 break loop 2610 default: 2611 t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrConnectionReset) 2612 } 2613 } 2614 // Expect the state to be StateError and subsequent Reads to fail with HardError. 2615 if _, _, err := c.EP.Read(nil); err != tcpip.ErrConnectionReset { 2616 t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrConnectionReset) 2617 } 2618 if tcp.EndpointState(c.EP.State()) != tcp.StateError { 2619 t.Fatalf("got EP state is not StateError") 2620 } 2621 } 2622 2623 func TestSendOnResetConnection(t *testing.T) { 2624 c := context.New(t, defaultMTU) 2625 defer c.Cleanup() 2626 2627 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 2628 2629 // Send RST segment. 2630 c.SendPacket(nil, &context.Headers{ 2631 SrcPort: context.TestPort, 2632 DstPort: c.Port, 2633 Flags: header.TCPFlagRst, 2634 SeqNum: 790, 2635 RcvWnd: 30000, 2636 }) 2637 2638 // Wait for the RST to be received. 2639 time.Sleep(1 * time.Second) 2640 2641 // Try to write. 2642 view := buffer.NewView(10) 2643 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != tcpip.ErrConnectionReset { 2644 t.Fatalf("got c.EP.Write(...) = %v, want = %v", err, tcpip.ErrConnectionReset) 2645 } 2646 } 2647 2648 func TestFinImmediately(t *testing.T) { 2649 c := context.New(t, defaultMTU) 2650 defer c.Cleanup() 2651 2652 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 2653 2654 // Shutdown immediately, check that we get a FIN. 2655 if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil { 2656 t.Fatalf("Shutdown failed: %v", err) 2657 } 2658 2659 checker.IPv4(t, c.GetPacket(), 2660 checker.PayloadLen(header.TCPMinimumSize), 2661 checker.TCP( 2662 checker.DstPort(context.TestPort), 2663 checker.SeqNum(uint32(c.IRS)+1), 2664 checker.AckNum(790), 2665 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin), 2666 ), 2667 ) 2668 2669 // Ack and send FIN as well. 2670 c.SendPacket(nil, &context.Headers{ 2671 SrcPort: context.TestPort, 2672 DstPort: c.Port, 2673 Flags: header.TCPFlagAck | header.TCPFlagFin, 2674 SeqNum: 790, 2675 AckNum: c.IRS.Add(2), 2676 RcvWnd: 30000, 2677 }) 2678 2679 // Check that the stack acks the FIN. 2680 checker.IPv4(t, c.GetPacket(), 2681 checker.PayloadLen(header.TCPMinimumSize), 2682 checker.TCP( 2683 checker.DstPort(context.TestPort), 2684 checker.SeqNum(uint32(c.IRS)+2), 2685 checker.AckNum(791), 2686 checker.TCPFlags(header.TCPFlagAck), 2687 ), 2688 ) 2689 } 2690 2691 func TestFinRetransmit(t *testing.T) { 2692 c := context.New(t, defaultMTU) 2693 defer c.Cleanup() 2694 2695 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 2696 2697 // Shutdown immediately, check that we get a FIN. 2698 if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil { 2699 t.Fatalf("Shutdown failed: %v", err) 2700 } 2701 2702 checker.IPv4(t, c.GetPacket(), 2703 checker.PayloadLen(header.TCPMinimumSize), 2704 checker.TCP( 2705 checker.DstPort(context.TestPort), 2706 checker.SeqNum(uint32(c.IRS)+1), 2707 checker.AckNum(790), 2708 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin), 2709 ), 2710 ) 2711 2712 // Don't acknowledge yet. We should get a retransmit of the FIN. 2713 checker.IPv4(t, c.GetPacket(), 2714 checker.PayloadLen(header.TCPMinimumSize), 2715 checker.TCP( 2716 checker.DstPort(context.TestPort), 2717 checker.SeqNum(uint32(c.IRS)+1), 2718 checker.AckNum(790), 2719 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin), 2720 ), 2721 ) 2722 2723 // Ack and send FIN as well. 2724 c.SendPacket(nil, &context.Headers{ 2725 SrcPort: context.TestPort, 2726 DstPort: c.Port, 2727 Flags: header.TCPFlagAck | header.TCPFlagFin, 2728 SeqNum: 790, 2729 AckNum: c.IRS.Add(2), 2730 RcvWnd: 30000, 2731 }) 2732 2733 // Check that the stack acks the FIN. 2734 checker.IPv4(t, c.GetPacket(), 2735 checker.PayloadLen(header.TCPMinimumSize), 2736 checker.TCP( 2737 checker.DstPort(context.TestPort), 2738 checker.SeqNum(uint32(c.IRS)+2), 2739 checker.AckNum(791), 2740 checker.TCPFlags(header.TCPFlagAck), 2741 ), 2742 ) 2743 } 2744 2745 func TestFinWithNoPendingData(t *testing.T) { 2746 c := context.New(t, defaultMTU) 2747 defer c.Cleanup() 2748 2749 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 2750 2751 // Write something out, and have it acknowledged. 2752 view := buffer.NewView(10) 2753 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 2754 t.Fatalf("Write failed: %v", err) 2755 } 2756 2757 next := uint32(c.IRS) + 1 2758 checker.IPv4(t, c.GetPacket(), 2759 checker.PayloadLen(len(view)+header.TCPMinimumSize), 2760 checker.TCP( 2761 checker.DstPort(context.TestPort), 2762 checker.SeqNum(next), 2763 checker.AckNum(790), 2764 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 2765 ), 2766 ) 2767 next += uint32(len(view)) 2768 2769 c.SendPacket(nil, &context.Headers{ 2770 SrcPort: context.TestPort, 2771 DstPort: c.Port, 2772 Flags: header.TCPFlagAck, 2773 SeqNum: 790, 2774 AckNum: seqnum.Value(next), 2775 RcvWnd: 30000, 2776 }) 2777 2778 // Shutdown, check that we get a FIN. 2779 if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil { 2780 t.Fatalf("Shutdown failed: %v", err) 2781 } 2782 2783 checker.IPv4(t, c.GetPacket(), 2784 checker.PayloadLen(header.TCPMinimumSize), 2785 checker.TCP( 2786 checker.DstPort(context.TestPort), 2787 checker.SeqNum(next), 2788 checker.AckNum(790), 2789 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin), 2790 ), 2791 ) 2792 next++ 2793 2794 // Ack and send FIN as well. 2795 c.SendPacket(nil, &context.Headers{ 2796 SrcPort: context.TestPort, 2797 DstPort: c.Port, 2798 Flags: header.TCPFlagAck | header.TCPFlagFin, 2799 SeqNum: 790, 2800 AckNum: seqnum.Value(next), 2801 RcvWnd: 30000, 2802 }) 2803 2804 // Check that the stack acks the FIN. 2805 checker.IPv4(t, c.GetPacket(), 2806 checker.PayloadLen(header.TCPMinimumSize), 2807 checker.TCP( 2808 checker.DstPort(context.TestPort), 2809 checker.SeqNum(next), 2810 checker.AckNum(791), 2811 checker.TCPFlags(header.TCPFlagAck), 2812 ), 2813 ) 2814 } 2815 2816 func DisabledTestFinWithPendingDataCwndFull(t *testing.T) { 2817 c := context.New(t, defaultMTU) 2818 defer c.Cleanup() 2819 2820 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 2821 2822 // Write enough segments to fill the congestion window before ACK'ing 2823 // any of them. 2824 view := buffer.NewView(10) 2825 for i := tcp.InitialCwnd; i > 0; i-- { 2826 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 2827 t.Fatalf("Write failed: %v", err) 2828 } 2829 } 2830 2831 next := uint32(c.IRS) + 1 2832 for i := tcp.InitialCwnd; i > 0; i-- { 2833 checker.IPv4(t, c.GetPacket(), 2834 checker.PayloadLen(len(view)+header.TCPMinimumSize), 2835 checker.TCP( 2836 checker.DstPort(context.TestPort), 2837 checker.SeqNum(next), 2838 checker.AckNum(790), 2839 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 2840 ), 2841 ) 2842 next += uint32(len(view)) 2843 } 2844 2845 // Shutdown the connection, check that the FIN segment isn't sent 2846 // because the congestion window doesn't allow it. Wait until a 2847 // retransmit is received. 2848 if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil { 2849 t.Fatalf("Shutdown failed: %v", err) 2850 } 2851 2852 checker.IPv4(t, c.GetPacket(), 2853 checker.PayloadLen(len(view)+header.TCPMinimumSize), 2854 checker.TCP( 2855 checker.DstPort(context.TestPort), 2856 checker.SeqNum(uint32(c.IRS)+1), 2857 checker.AckNum(790), 2858 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 2859 ), 2860 ) 2861 2862 // Send the ACK that will allow the FIN to be sent as well. 2863 c.SendPacket(nil, &context.Headers{ 2864 SrcPort: context.TestPort, 2865 DstPort: c.Port, 2866 Flags: header.TCPFlagAck, 2867 SeqNum: 790, 2868 AckNum: seqnum.Value(next), 2869 RcvWnd: 30000, 2870 }) 2871 2872 checker.IPv4(t, c.GetPacket(), 2873 checker.PayloadLen(header.TCPMinimumSize), 2874 checker.TCP( 2875 checker.DstPort(context.TestPort), 2876 checker.SeqNum(next), 2877 checker.AckNum(790), 2878 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin), 2879 ), 2880 ) 2881 next++ 2882 2883 // Send a FIN that acknowledges everything. Get an ACK back. 2884 c.SendPacket(nil, &context.Headers{ 2885 SrcPort: context.TestPort, 2886 DstPort: c.Port, 2887 Flags: header.TCPFlagAck | header.TCPFlagFin, 2888 SeqNum: 790, 2889 AckNum: seqnum.Value(next), 2890 RcvWnd: 30000, 2891 }) 2892 2893 checker.IPv4(t, c.GetPacket(), 2894 checker.PayloadLen(header.TCPMinimumSize), 2895 checker.TCP( 2896 checker.DstPort(context.TestPort), 2897 checker.SeqNum(next), 2898 checker.AckNum(791), 2899 checker.TCPFlags(header.TCPFlagAck), 2900 ), 2901 ) 2902 } 2903 2904 func TestFinWithPendingData(t *testing.T) { 2905 c := context.New(t, defaultMTU) 2906 defer c.Cleanup() 2907 2908 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 2909 2910 // Write something out, and acknowledge it to get cwnd to 2. 2911 view := buffer.NewView(10) 2912 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 2913 t.Fatalf("Write failed: %v", err) 2914 } 2915 2916 next := uint32(c.IRS) + 1 2917 checker.IPv4(t, c.GetPacket(), 2918 checker.PayloadLen(len(view)+header.TCPMinimumSize), 2919 checker.TCP( 2920 checker.DstPort(context.TestPort), 2921 checker.SeqNum(next), 2922 checker.AckNum(790), 2923 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 2924 ), 2925 ) 2926 next += uint32(len(view)) 2927 2928 c.SendPacket(nil, &context.Headers{ 2929 SrcPort: context.TestPort, 2930 DstPort: c.Port, 2931 Flags: header.TCPFlagAck, 2932 SeqNum: 790, 2933 AckNum: seqnum.Value(next), 2934 RcvWnd: 30000, 2935 }) 2936 2937 // Write new data, but don't acknowledge it. 2938 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 2939 t.Fatalf("Write failed: %v", err) 2940 } 2941 2942 checker.IPv4(t, c.GetPacket(), 2943 checker.PayloadLen(len(view)+header.TCPMinimumSize), 2944 checker.TCP( 2945 checker.DstPort(context.TestPort), 2946 checker.SeqNum(next), 2947 checker.AckNum(790), 2948 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 2949 ), 2950 ) 2951 next += uint32(len(view)) 2952 2953 // Shutdown the connection, check that we do get a FIN. 2954 if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil { 2955 t.Fatalf("Shutdown failed: %v", err) 2956 } 2957 2958 checker.IPv4(t, c.GetPacket(), 2959 checker.PayloadLen(header.TCPMinimumSize), 2960 checker.TCP( 2961 checker.DstPort(context.TestPort), 2962 checker.SeqNum(next), 2963 checker.AckNum(790), 2964 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin), 2965 ), 2966 ) 2967 next++ 2968 2969 // Send a FIN that acknowledges everything. Get an ACK back. 2970 c.SendPacket(nil, &context.Headers{ 2971 SrcPort: context.TestPort, 2972 DstPort: c.Port, 2973 Flags: header.TCPFlagAck | header.TCPFlagFin, 2974 SeqNum: 790, 2975 AckNum: seqnum.Value(next), 2976 RcvWnd: 30000, 2977 }) 2978 2979 checker.IPv4(t, c.GetPacket(), 2980 checker.PayloadLen(header.TCPMinimumSize), 2981 checker.TCP( 2982 checker.DstPort(context.TestPort), 2983 checker.SeqNum(next), 2984 checker.AckNum(791), 2985 checker.TCPFlags(header.TCPFlagAck), 2986 ), 2987 ) 2988 } 2989 2990 func TestFinWithPartialAck(t *testing.T) { 2991 c := context.New(t, defaultMTU) 2992 defer c.Cleanup() 2993 2994 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 2995 2996 // Write something out, and acknowledge it to get cwnd to 2. Also send 2997 // FIN from the test side. 2998 view := buffer.NewView(10) 2999 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 3000 t.Fatalf("Write failed: %v", err) 3001 } 3002 3003 next := uint32(c.IRS) + 1 3004 checker.IPv4(t, c.GetPacket(), 3005 checker.PayloadLen(len(view)+header.TCPMinimumSize), 3006 checker.TCP( 3007 checker.DstPort(context.TestPort), 3008 checker.SeqNum(next), 3009 checker.AckNum(790), 3010 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 3011 ), 3012 ) 3013 next += uint32(len(view)) 3014 3015 c.SendPacket(nil, &context.Headers{ 3016 SrcPort: context.TestPort, 3017 DstPort: c.Port, 3018 Flags: header.TCPFlagAck | header.TCPFlagFin, 3019 SeqNum: 790, 3020 AckNum: seqnum.Value(next), 3021 RcvWnd: 30000, 3022 }) 3023 3024 // Check that we get an ACK for the fin. 3025 checker.IPv4(t, c.GetPacket(), 3026 checker.PayloadLen(header.TCPMinimumSize), 3027 checker.TCP( 3028 checker.DstPort(context.TestPort), 3029 checker.SeqNum(next), 3030 checker.AckNum(791), 3031 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 3032 ), 3033 ) 3034 3035 // Write new data, but don't acknowledge it. 3036 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 3037 t.Fatalf("Write failed: %v", err) 3038 } 3039 3040 checker.IPv4(t, c.GetPacket(), 3041 checker.PayloadLen(len(view)+header.TCPMinimumSize), 3042 checker.TCP( 3043 checker.DstPort(context.TestPort), 3044 checker.SeqNum(next), 3045 checker.AckNum(791), 3046 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 3047 ), 3048 ) 3049 next += uint32(len(view)) 3050 3051 // Shutdown the connection, check that we do get a FIN. 3052 if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil { 3053 t.Fatalf("Shutdown failed: %v", err) 3054 } 3055 3056 checker.IPv4(t, c.GetPacket(), 3057 checker.PayloadLen(header.TCPMinimumSize), 3058 checker.TCP( 3059 checker.DstPort(context.TestPort), 3060 checker.SeqNum(next), 3061 checker.AckNum(791), 3062 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin), 3063 ), 3064 ) 3065 next++ 3066 3067 // Send an ACK for the data, but not for the FIN yet. 3068 c.SendPacket(nil, &context.Headers{ 3069 SrcPort: context.TestPort, 3070 DstPort: c.Port, 3071 Flags: header.TCPFlagAck, 3072 SeqNum: 791, 3073 AckNum: seqnum.Value(next - 1), 3074 RcvWnd: 30000, 3075 }) 3076 3077 // Check that we don't get a retransmit of the FIN. 3078 c.CheckNoPacketTimeout("FIN retransmitted when data was ack'd", 100*time.Millisecond) 3079 3080 // Ack the FIN. 3081 c.SendPacket(nil, &context.Headers{ 3082 SrcPort: context.TestPort, 3083 DstPort: c.Port, 3084 Flags: header.TCPFlagAck | header.TCPFlagFin, 3085 SeqNum: 791, 3086 AckNum: seqnum.Value(next), 3087 RcvWnd: 30000, 3088 }) 3089 } 3090 3091 func TestUpdateListenBacklog(t *testing.T) { 3092 c := context.New(t, defaultMTU) 3093 defer c.Cleanup() 3094 3095 // Create listener. 3096 var wq waiter.Queue 3097 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq) 3098 if err != nil { 3099 t.Fatalf("NewEndpoint failed: %v", err) 3100 } 3101 3102 if err := ep.Bind(tcpip.FullAddress{}); err != nil { 3103 t.Fatalf("Bind failed: %v", err) 3104 } 3105 3106 if err := ep.Listen(10); err != nil { 3107 t.Fatalf("Listen failed: %v", err) 3108 } 3109 3110 // Update the backlog with another Listen() on the same endpoint. 3111 if err := ep.Listen(20); err != nil { 3112 t.Fatalf("Listen failed to update backlog: %v", err) 3113 } 3114 3115 ep.Close() 3116 } 3117 3118 func scaledSendWindow(t *testing.T, scale uint8) { 3119 // This test ensures that the endpoint is using the right scaling by 3120 // sending a buffer that is larger than the window size, and ensuring 3121 // that the endpoint doesn't send more than allowed. 3122 c := context.New(t, defaultMTU) 3123 defer c.Cleanup() 3124 3125 maxPayload := defaultMTU - header.IPv4MinimumSize - header.TCPMinimumSize 3126 c.CreateConnectedWithRawOptions(789, 0, -1 /* epRcvBuf */, []byte{ 3127 header.TCPOptionMSS, 4, byte(maxPayload / 256), byte(maxPayload % 256), 3128 header.TCPOptionWS, 3, scale, header.TCPOptionNOP, 3129 }) 3130 3131 // Open up the window with a scaled value. 3132 c.SendPacket(nil, &context.Headers{ 3133 SrcPort: context.TestPort, 3134 DstPort: c.Port, 3135 Flags: header.TCPFlagAck, 3136 SeqNum: 790, 3137 AckNum: c.IRS.Add(1), 3138 RcvWnd: 1, 3139 }) 3140 3141 // Send some data. Check that it's capped by the window size. 3142 view := buffer.NewView(65535) 3143 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 3144 t.Fatalf("Write failed: %v", err) 3145 } 3146 3147 // Check that only data that fits in the scaled window is sent. 3148 checker.IPv4(t, c.GetPacket(), 3149 checker.PayloadLen((1<<scale)+header.TCPMinimumSize), 3150 checker.TCP( 3151 checker.DstPort(context.TestPort), 3152 checker.SeqNum(uint32(c.IRS)+1), 3153 checker.AckNum(790), 3154 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 3155 ), 3156 ) 3157 3158 // Reset the connection to free resources. 3159 c.SendPacket(nil, &context.Headers{ 3160 SrcPort: context.TestPort, 3161 DstPort: c.Port, 3162 Flags: header.TCPFlagRst, 3163 SeqNum: 790, 3164 }) 3165 } 3166 3167 func TestScaledSendWindow(t *testing.T) { 3168 for scale := uint8(0); scale <= 14; scale++ { 3169 scaledSendWindow(t, scale) 3170 } 3171 } 3172 3173 func TestReceivedValidSegmentCountIncrement(t *testing.T) { 3174 c := context.New(t, defaultMTU) 3175 defer c.Cleanup() 3176 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 3177 stats := c.Stack().Stats() 3178 want := stats.TCP.ValidSegmentsReceived.Value() + 1 3179 3180 c.SendPacket(nil, &context.Headers{ 3181 SrcPort: context.TestPort, 3182 DstPort: c.Port, 3183 Flags: header.TCPFlagAck, 3184 SeqNum: seqnum.Value(790), 3185 AckNum: c.IRS.Add(1), 3186 RcvWnd: 30000, 3187 }) 3188 3189 if got := stats.TCP.ValidSegmentsReceived.Value(); got != want { 3190 t.Errorf("got stats.TCP.ValidSegmentsReceived.Value() = %v, want = %v", got, want) 3191 } 3192 if got := c.EP.Stats().(*tcp.Stats).SegmentsReceived.Value(); got != want { 3193 t.Errorf("got EP stats Stats.SegmentsReceived = %v, want = %v", got, want) 3194 } 3195 // Ensure there were no errors during handshake. If these stats have 3196 // incremented, then the connection should not have been established. 3197 if got := c.EP.Stats().(*tcp.Stats).SendErrors.NoRoute.Value(); got != 0 { 3198 t.Errorf("got EP stats Stats.SendErrors.NoRoute = %v, want = %v", got, 0) 3199 } 3200 if got := c.EP.Stats().(*tcp.Stats).SendErrors.NoLinkAddr.Value(); got != 0 { 3201 t.Errorf("got EP stats Stats.SendErrors.NoLinkAddr = %v, want = %v", got, 0) 3202 } 3203 } 3204 3205 func TestReceivedInvalidSegmentCountIncrement(t *testing.T) { 3206 c := context.New(t, defaultMTU) 3207 defer c.Cleanup() 3208 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 3209 stats := c.Stack().Stats() 3210 want := stats.TCP.InvalidSegmentsReceived.Value() + 1 3211 vv := c.BuildSegment(nil, &context.Headers{ 3212 SrcPort: context.TestPort, 3213 DstPort: c.Port, 3214 Flags: header.TCPFlagAck, 3215 SeqNum: seqnum.Value(790), 3216 AckNum: c.IRS.Add(1), 3217 RcvWnd: 30000, 3218 }) 3219 tcpbuf := vv.First()[header.IPv4MinimumSize:] 3220 tcpbuf[header.TCPDataOffset] = ((header.TCPMinimumSize - 1) / 4) << 4 3221 3222 c.SendSegment(vv) 3223 3224 if got := stats.TCP.InvalidSegmentsReceived.Value(); got != want { 3225 t.Errorf("got stats.TCP.InvalidSegmentsReceived.Value() = %v, want = %v", got, want) 3226 } 3227 if got := c.EP.Stats().(*tcp.Stats).ReceiveErrors.MalformedPacketsReceived.Value(); got != want { 3228 t.Errorf("got EP Stats.ReceiveErrors.MalformedPacketsReceived stats = %v, want = %v", got, want) 3229 } 3230 } 3231 3232 func TestReceivedIncorrectChecksumIncrement(t *testing.T) { 3233 c := context.New(t, defaultMTU) 3234 defer c.Cleanup() 3235 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 3236 stats := c.Stack().Stats() 3237 want := stats.TCP.ChecksumErrors.Value() + 1 3238 vv := c.BuildSegment([]byte{0x1, 0x2, 0x3}, &context.Headers{ 3239 SrcPort: context.TestPort, 3240 DstPort: c.Port, 3241 Flags: header.TCPFlagAck, 3242 SeqNum: seqnum.Value(790), 3243 AckNum: c.IRS.Add(1), 3244 RcvWnd: 30000, 3245 }) 3246 tcpbuf := vv.First()[header.IPv4MinimumSize:] 3247 // Overwrite a byte in the payload which should cause checksum 3248 // verification to fail. 3249 tcpbuf[(tcpbuf[header.TCPDataOffset]>>4)*4] = 0x4 3250 3251 c.SendSegment(vv) 3252 3253 if got := stats.TCP.ChecksumErrors.Value(); got != want { 3254 t.Errorf("got stats.TCP.ChecksumErrors.Value() = %d, want = %d", got, want) 3255 } 3256 if got := c.EP.Stats().(*tcp.Stats).ReceiveErrors.ChecksumErrors.Value(); got != want { 3257 t.Errorf("got EP stats Stats.ReceiveErrors.ChecksumErrors = %d, want = %d", got, want) 3258 } 3259 } 3260 3261 func TestReceivedSegmentQueuing(t *testing.T) { 3262 // This test sends 200 segments containing a few bytes each to an 3263 // endpoint and checks that they're all received and acknowledged by 3264 // the endpoint, that is, that none of the segments are dropped by 3265 // internal queues. 3266 c := context.New(t, defaultMTU) 3267 defer c.Cleanup() 3268 3269 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 3270 3271 // Send 200 segments. 3272 data := []byte{1, 2, 3} 3273 for i := 0; i < 200; i++ { 3274 c.SendPacket(data, &context.Headers{ 3275 SrcPort: context.TestPort, 3276 DstPort: c.Port, 3277 Flags: header.TCPFlagAck, 3278 SeqNum: seqnum.Value(790 + i*len(data)), 3279 AckNum: c.IRS.Add(1), 3280 RcvWnd: 30000, 3281 }) 3282 } 3283 3284 // Receive ACKs for all segments. 3285 last := seqnum.Value(790 + 200*len(data)) 3286 for { 3287 b := c.GetPacket() 3288 checker.IPv4(t, b, 3289 checker.TCP( 3290 checker.DstPort(context.TestPort), 3291 checker.SeqNum(uint32(c.IRS)+1), 3292 checker.TCPFlags(header.TCPFlagAck), 3293 ), 3294 ) 3295 tcpHdr := header.TCP(header.IPv4(b).Payload()) 3296 ack := seqnum.Value(tcpHdr.AckNumber()) 3297 if ack == last { 3298 break 3299 } 3300 3301 if last.LessThan(ack) { 3302 t.Fatalf("Acknowledge (%v) beyond the expected (%v)", ack, last) 3303 } 3304 } 3305 } 3306 3307 func TestReadAfterClosedState(t *testing.T) { 3308 // This test ensures that calling Read() or Peek() after the endpoint 3309 // has transitioned to closedState still works if there is pending 3310 // data. To transition to stateClosed without calling Close(), we must 3311 // shutdown the send path and the peer must send its own FIN. 3312 c := context.New(t, defaultMTU) 3313 defer c.Cleanup() 3314 3315 // Set TCPTimeWaitTimeout to 1 seconds so that sockets are marked closed 3316 // after 1 second in TIME_WAIT state. 3317 tcpTimeWaitTimeout := 1 * time.Second 3318 if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, tcpip.TCPTimeWaitTimeoutOption(tcpTimeWaitTimeout)); err != nil { 3319 t.Fatalf("c.stack.SetTransportProtocolOption(tcp, tcpip.TCPTimeWaitTimeout(%d) failed: %s", tcpTimeWaitTimeout, err) 3320 } 3321 3322 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 3323 3324 we, ch := waiter.NewChannelEntry(nil) 3325 c.WQ.EventRegister(&we, waiter.EventIn) 3326 defer c.WQ.EventUnregister(&we) 3327 3328 if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock { 3329 t.Fatalf("got c.EP.Read(nil) = %v, want = %s", err, tcpip.ErrWouldBlock) 3330 } 3331 3332 // Shutdown immediately for write, check that we get a FIN. 3333 if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil { 3334 t.Fatalf("Shutdown failed: %s", err) 3335 } 3336 3337 checker.IPv4(t, c.GetPacket(), 3338 checker.PayloadLen(header.TCPMinimumSize), 3339 checker.TCP( 3340 checker.DstPort(context.TestPort), 3341 checker.SeqNum(uint32(c.IRS)+1), 3342 checker.AckNum(790), 3343 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin), 3344 ), 3345 ) 3346 3347 if got, want := tcp.EndpointState(c.EP.State()), tcp.StateFinWait1; got != want { 3348 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 3349 } 3350 3351 // Send some data and acknowledge the FIN. 3352 data := []byte{1, 2, 3} 3353 c.SendPacket(data, &context.Headers{ 3354 SrcPort: context.TestPort, 3355 DstPort: c.Port, 3356 Flags: header.TCPFlagAck | header.TCPFlagFin, 3357 SeqNum: 790, 3358 AckNum: c.IRS.Add(2), 3359 RcvWnd: 30000, 3360 }) 3361 3362 // Check that ACK is received. 3363 checker.IPv4(t, c.GetPacket(), 3364 checker.TCP( 3365 checker.DstPort(context.TestPort), 3366 checker.SeqNum(uint32(c.IRS)+2), 3367 checker.AckNum(uint32(791+len(data))), 3368 checker.TCPFlags(header.TCPFlagAck), 3369 ), 3370 ) 3371 3372 // Give the stack the chance to transition to closed state from 3373 // TIME_WAIT. 3374 time.Sleep(tcpTimeWaitTimeout * 2) 3375 3376 if got, want := tcp.EndpointState(c.EP.State()), tcp.StateClose; got != want { 3377 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 3378 } 3379 3380 // Wait for receive to be notified. 3381 select { 3382 case <-ch: 3383 case <-time.After(1 * time.Second): 3384 t.Fatalf("Timed out waiting for data to arrive") 3385 } 3386 3387 // Check that peek works. 3388 peekBuf := make([]byte, 10) 3389 n, _, err := c.EP.Peek([][]byte{peekBuf}) 3390 if err != nil { 3391 t.Fatalf("Peek failed: %s", err) 3392 } 3393 3394 peekBuf = peekBuf[:n] 3395 if !bytes.Equal(data, peekBuf) { 3396 t.Fatalf("got data = %v, want = %v", peekBuf, data) 3397 } 3398 3399 // Receive data. 3400 v, _, err := c.EP.Read(nil) 3401 if err != nil { 3402 t.Fatalf("Read failed: %s", err) 3403 } 3404 3405 if !bytes.Equal(data, v) { 3406 t.Fatalf("got data = %v, want = %v", v, data) 3407 } 3408 3409 // Now that we drained the queue, check that functions fail with the 3410 // right error code. 3411 if _, _, err := c.EP.Read(nil); err != tcpip.ErrClosedForReceive { 3412 t.Fatalf("got c.EP.Read(nil) = %v, want = %s", err, tcpip.ErrClosedForReceive) 3413 } 3414 3415 if _, _, err := c.EP.Peek([][]byte{peekBuf}); err != tcpip.ErrClosedForReceive { 3416 t.Fatalf("got c.EP.Peek(...) = %v, want = %s", err, tcpip.ErrClosedForReceive) 3417 } 3418 } 3419 3420 func TestReusePort(t *testing.T) { 3421 // This test ensures that ports are immediately available for reuse 3422 // after Close on the endpoints using them returns. 3423 c := context.New(t, defaultMTU) 3424 defer c.Cleanup() 3425 3426 // First case, just an endpoint that was bound. 3427 var err *tcpip.Error 3428 c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{}) 3429 if err != nil { 3430 t.Fatalf("NewEndpoint failed; %v", err) 3431 } 3432 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 3433 t.Fatalf("Bind failed: %v", err) 3434 } 3435 3436 c.EP.Close() 3437 c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{}) 3438 if err != nil { 3439 t.Fatalf("NewEndpoint failed; %v", err) 3440 } 3441 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 3442 t.Fatalf("Bind failed: %v", err) 3443 } 3444 c.EP.Close() 3445 3446 // Second case, an endpoint that was bound and is connecting.. 3447 c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{}) 3448 if err != nil { 3449 t.Fatalf("NewEndpoint failed; %v", err) 3450 } 3451 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 3452 t.Fatalf("Bind failed: %v", err) 3453 } 3454 if err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrConnectStarted { 3455 t.Fatalf("got c.EP.Connect(...) = %v, want = %v", err, tcpip.ErrConnectStarted) 3456 } 3457 c.EP.Close() 3458 3459 c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{}) 3460 if err != nil { 3461 t.Fatalf("NewEndpoint failed; %v", err) 3462 } 3463 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 3464 t.Fatalf("Bind failed: %v", err) 3465 } 3466 c.EP.Close() 3467 3468 // Third case, an endpoint that was bound and is listening. 3469 c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{}) 3470 if err != nil { 3471 t.Fatalf("NewEndpoint failed; %v", err) 3472 } 3473 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 3474 t.Fatalf("Bind failed: %v", err) 3475 } 3476 if err := c.EP.Listen(10); err != nil { 3477 t.Fatalf("Listen failed: %v", err) 3478 } 3479 c.EP.Close() 3480 3481 c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{}) 3482 if err != nil { 3483 t.Fatalf("NewEndpoint failed; %v", err) 3484 } 3485 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 3486 t.Fatalf("Bind failed: %v", err) 3487 } 3488 if err := c.EP.Listen(10); err != nil { 3489 t.Fatalf("Listen failed: %v", err) 3490 } 3491 } 3492 3493 func checkRecvBufferSize(t *testing.T, ep tcpip.Endpoint, v int) { 3494 t.Helper() 3495 3496 s, err := ep.GetSockOptInt(tcpip.ReceiveBufferSizeOption) 3497 if err != nil { 3498 t.Fatalf("GetSockOpt failed: %v", err) 3499 } 3500 3501 if int(s) != v { 3502 t.Fatalf("got receive buffer size = %v, want = %v", s, v) 3503 } 3504 } 3505 3506 func checkSendBufferSize(t *testing.T, ep tcpip.Endpoint, v int) { 3507 t.Helper() 3508 3509 s, err := ep.GetSockOptInt(tcpip.SendBufferSizeOption) 3510 if err != nil { 3511 t.Fatalf("GetSockOpt failed: %v", err) 3512 } 3513 3514 if int(s) != v { 3515 t.Fatalf("got send buffer size = %v, want = %v", s, v) 3516 } 3517 } 3518 3519 func TestDefaultBufferSizes(t *testing.T) { 3520 s := stack.New(stack.Options{ 3521 NetworkProtocols: []stack.NetworkProtocol{ipv4.NewProtocol()}, 3522 TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol()}, 3523 }) 3524 3525 // Check the default values. 3526 ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{}) 3527 if err != nil { 3528 t.Fatalf("NewEndpoint failed; %v", err) 3529 } 3530 defer func() { 3531 if ep != nil { 3532 ep.Close() 3533 } 3534 }() 3535 3536 checkSendBufferSize(t, ep, tcp.DefaultSendBufferSize) 3537 checkRecvBufferSize(t, ep, tcp.DefaultReceiveBufferSize) 3538 3539 // Change the default send buffer size. 3540 if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, tcp.SendBufferSizeOption{1, tcp.DefaultSendBufferSize * 2, tcp.DefaultSendBufferSize * 20}); err != nil { 3541 t.Fatalf("SetTransportProtocolOption failed: %v", err) 3542 } 3543 3544 ep.Close() 3545 ep, err = s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{}) 3546 if err != nil { 3547 t.Fatalf("NewEndpoint failed; %v", err) 3548 } 3549 3550 checkSendBufferSize(t, ep, tcp.DefaultSendBufferSize*2) 3551 checkRecvBufferSize(t, ep, tcp.DefaultReceiveBufferSize) 3552 3553 // Change the default receive buffer size. 3554 if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, tcp.ReceiveBufferSizeOption{1, tcp.DefaultReceiveBufferSize * 3, tcp.DefaultReceiveBufferSize * 30}); err != nil { 3555 t.Fatalf("SetTransportProtocolOption failed: %v", err) 3556 } 3557 3558 ep.Close() 3559 ep, err = s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{}) 3560 if err != nil { 3561 t.Fatalf("NewEndpoint failed; %v", err) 3562 } 3563 3564 checkSendBufferSize(t, ep, tcp.DefaultSendBufferSize*2) 3565 checkRecvBufferSize(t, ep, tcp.DefaultReceiveBufferSize*3) 3566 } 3567 3568 func TestMinMaxBufferSizes(t *testing.T) { 3569 s := stack.New(stack.Options{ 3570 NetworkProtocols: []stack.NetworkProtocol{ipv4.NewProtocol()}, 3571 TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol()}, 3572 }) 3573 3574 // Check the default values. 3575 ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{}) 3576 if err != nil { 3577 t.Fatalf("NewEndpoint failed; %v", err) 3578 } 3579 defer ep.Close() 3580 3581 // Change the min/max values for send/receive 3582 if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, tcp.ReceiveBufferSizeOption{200, tcp.DefaultReceiveBufferSize * 2, tcp.DefaultReceiveBufferSize * 20}); err != nil { 3583 t.Fatalf("SetTransportProtocolOption failed: %v", err) 3584 } 3585 3586 if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, tcp.SendBufferSizeOption{300, tcp.DefaultSendBufferSize * 3, tcp.DefaultSendBufferSize * 30}); err != nil { 3587 t.Fatalf("SetTransportProtocolOption failed: %v", err) 3588 } 3589 3590 // Set values below the min. 3591 if err := ep.SetSockOptInt(tcpip.ReceiveBufferSizeOption, 199); err != nil { 3592 t.Fatalf("GetSockOpt failed: %v", err) 3593 } 3594 3595 checkRecvBufferSize(t, ep, 200) 3596 3597 if err := ep.SetSockOptInt(tcpip.SendBufferSizeOption, 299); err != nil { 3598 t.Fatalf("GetSockOpt failed: %v", err) 3599 } 3600 3601 checkSendBufferSize(t, ep, 300) 3602 3603 // Set values above the max. 3604 if err := ep.SetSockOptInt(tcpip.ReceiveBufferSizeOption, 1+tcp.DefaultReceiveBufferSize*20); err != nil { 3605 t.Fatalf("GetSockOpt failed: %v", err) 3606 } 3607 3608 checkRecvBufferSize(t, ep, tcp.DefaultReceiveBufferSize*20) 3609 3610 if err := ep.SetSockOptInt(tcpip.SendBufferSizeOption, 1+tcp.DefaultSendBufferSize*30); err != nil { 3611 t.Fatalf("GetSockOpt failed: %v", err) 3612 } 3613 3614 checkSendBufferSize(t, ep, tcp.DefaultSendBufferSize*30) 3615 } 3616 3617 func TestBindToDeviceOption(t *testing.T) { 3618 s := stack.New(stack.Options{ 3619 NetworkProtocols: []stack.NetworkProtocol{ipv4.NewProtocol()}, 3620 TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol()}}) 3621 3622 ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{}) 3623 if err != nil { 3624 t.Fatalf("NewEndpoint failed; %v", err) 3625 } 3626 defer ep.Close() 3627 3628 if err := s.CreateNamedNIC(321, "my_device", loopback.New()); err != nil { 3629 t.Errorf("CreateNamedNIC failed: %v", err) 3630 } 3631 3632 // Make an nameless NIC. 3633 if err := s.CreateNIC(54321, loopback.New()); err != nil { 3634 t.Errorf("CreateNIC failed: %v", err) 3635 } 3636 3637 // strPtr is used instead of taking the address of string literals, which is 3638 // a compiler error. 3639 strPtr := func(s string) *string { 3640 return &s 3641 } 3642 3643 testActions := []struct { 3644 name string 3645 setBindToDevice *string 3646 setBindToDeviceError *tcpip.Error 3647 getBindToDevice tcpip.BindToDeviceOption 3648 }{ 3649 {"GetDefaultValue", nil, nil, ""}, 3650 {"BindToNonExistent", strPtr("non_existent_device"), tcpip.ErrUnknownDevice, ""}, 3651 {"BindToExistent", strPtr("my_device"), nil, "my_device"}, 3652 {"UnbindToDevice", strPtr(""), nil, ""}, 3653 } 3654 for _, testAction := range testActions { 3655 t.Run(testAction.name, func(t *testing.T) { 3656 if testAction.setBindToDevice != nil { 3657 bindToDevice := tcpip.BindToDeviceOption(*testAction.setBindToDevice) 3658 if got, want := ep.SetSockOpt(bindToDevice), testAction.setBindToDeviceError; got != want { 3659 t.Errorf("SetSockOpt(%v) got %v, want %v", bindToDevice, got, want) 3660 } 3661 } 3662 bindToDevice := tcpip.BindToDeviceOption("to be modified by GetSockOpt") 3663 if ep.GetSockOpt(&bindToDevice) != nil { 3664 t.Errorf("GetSockOpt got %v, want %v", ep.GetSockOpt(&bindToDevice), nil) 3665 } 3666 if got, want := bindToDevice, testAction.getBindToDevice; got != want { 3667 t.Errorf("bindToDevice got %q, want %q", got, want) 3668 } 3669 }) 3670 } 3671 } 3672 3673 func makeStack() (*stack.Stack, *tcpip.Error) { 3674 s := stack.New(stack.Options{ 3675 NetworkProtocols: []stack.NetworkProtocol{ 3676 ipv4.NewProtocol(), 3677 ipv6.NewProtocol(), 3678 }, 3679 TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol()}, 3680 }) 3681 3682 id := loopback.New() 3683 if testing.Verbose() { 3684 id = sniffer.New(id) 3685 } 3686 3687 if err := s.CreateNIC(1, id); err != nil { 3688 return nil, err 3689 } 3690 3691 for _, ct := range []struct { 3692 number tcpip.NetworkProtocolNumber 3693 address tcpip.Address 3694 }{ 3695 {ipv4.ProtocolNumber, context.StackAddr}, 3696 {ipv6.ProtocolNumber, context.StackV6Addr}, 3697 } { 3698 if err := s.AddAddress(1, ct.number, ct.address); err != nil { 3699 return nil, err 3700 } 3701 } 3702 3703 s.SetRouteTable([]tcpip.Route{ 3704 { 3705 Destination: header.IPv4EmptySubnet, 3706 NIC: 1, 3707 }, 3708 { 3709 Destination: header.IPv6EmptySubnet, 3710 NIC: 1, 3711 }, 3712 }) 3713 3714 return s, nil 3715 } 3716 3717 func TestSelfConnect(t *testing.T) { 3718 // This test ensures that intentional self-connects work. In particular, 3719 // it checks that if an endpoint binds to say 127.0.0.1:1000 then 3720 // connects to 127.0.0.1:1000, then it will be connected to itself, and 3721 // is able to send and receive data through the same endpoint. 3722 s, err := makeStack() 3723 if err != nil { 3724 t.Fatal(err) 3725 } 3726 3727 var wq waiter.Queue 3728 ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq) 3729 if err != nil { 3730 t.Fatalf("NewEndpoint failed: %v", err) 3731 } 3732 defer ep.Close() 3733 3734 if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 3735 t.Fatalf("Bind failed: %v", err) 3736 } 3737 3738 // Register for notification, then start connection attempt. 3739 waitEntry, notifyCh := waiter.NewChannelEntry(nil) 3740 wq.EventRegister(&waitEntry, waiter.EventOut) 3741 defer wq.EventUnregister(&waitEntry) 3742 3743 if err := ep.Connect(tcpip.FullAddress{Addr: context.StackAddr, Port: context.StackPort}); err != tcpip.ErrConnectStarted { 3744 t.Fatalf("got ep.Connect(...) = %v, want = %v", err, tcpip.ErrConnectStarted) 3745 } 3746 3747 <-notifyCh 3748 if err := ep.GetSockOpt(tcpip.ErrorOption{}); err != nil { 3749 t.Fatalf("Connect failed: %v", err) 3750 } 3751 3752 // Write something. 3753 data := []byte{1, 2, 3} 3754 view := buffer.NewView(len(data)) 3755 copy(view, data) 3756 if _, _, err := ep.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 3757 t.Fatalf("Write failed: %v", err) 3758 } 3759 3760 // Read back what was written. 3761 wq.EventUnregister(&waitEntry) 3762 wq.EventRegister(&waitEntry, waiter.EventIn) 3763 rd, _, err := ep.Read(nil) 3764 if err != nil { 3765 if err != tcpip.ErrWouldBlock { 3766 t.Fatalf("Read failed: %v", err) 3767 } 3768 <-notifyCh 3769 rd, _, err = ep.Read(nil) 3770 if err != nil { 3771 t.Fatalf("Read failed: %v", err) 3772 } 3773 } 3774 3775 if !bytes.Equal(data, rd) { 3776 t.Fatalf("got data = %v, want = %v", rd, data) 3777 } 3778 } 3779 3780 func TestConnectAvoidsBoundPorts(t *testing.T) { 3781 addressTypes := func(t *testing.T, network string) []string { 3782 switch network { 3783 case "ipv4": 3784 return []string{"v4"} 3785 case "ipv6": 3786 return []string{"v6"} 3787 case "dual": 3788 return []string{"v6", "mapped"} 3789 default: 3790 t.Fatalf("unknown network: '%s'", network) 3791 } 3792 3793 panic("unreachable") 3794 } 3795 3796 address := func(t *testing.T, addressType string, isAny bool) tcpip.Address { 3797 switch addressType { 3798 case "v4": 3799 if isAny { 3800 return "" 3801 } 3802 return context.StackAddr 3803 case "v6": 3804 if isAny { 3805 return "" 3806 } 3807 return context.StackV6Addr 3808 case "mapped": 3809 if isAny { 3810 return context.V4MappedWildcardAddr 3811 } 3812 return context.StackV4MappedAddr 3813 default: 3814 t.Fatalf("unknown address type: '%s'", addressType) 3815 } 3816 3817 panic("unreachable") 3818 } 3819 // This test ensures that Endpoint.Connect doesn't select already-bound ports. 3820 networks := []string{"ipv4", "ipv6", "dual"} 3821 for _, exhaustedNetwork := range networks { 3822 t.Run(fmt.Sprintf("exhaustedNetwork=%s", exhaustedNetwork), func(t *testing.T) { 3823 for _, exhaustedAddressType := range addressTypes(t, exhaustedNetwork) { 3824 t.Run(fmt.Sprintf("exhaustedAddressType=%s", exhaustedAddressType), func(t *testing.T) { 3825 for _, isAny := range []bool{false, true} { 3826 t.Run(fmt.Sprintf("isAny=%t", isAny), func(t *testing.T) { 3827 for _, candidateNetwork := range networks { 3828 t.Run(fmt.Sprintf("candidateNetwork=%s", candidateNetwork), func(t *testing.T) { 3829 for _, candidateAddressType := range addressTypes(t, candidateNetwork) { 3830 t.Run(fmt.Sprintf("candidateAddressType=%s", candidateAddressType), func(t *testing.T) { 3831 s, err := makeStack() 3832 if err != nil { 3833 t.Fatal(err) 3834 } 3835 3836 var wq waiter.Queue 3837 var eps []tcpip.Endpoint 3838 defer func() { 3839 for _, ep := range eps { 3840 ep.Close() 3841 } 3842 }() 3843 makeEP := func(network string) tcpip.Endpoint { 3844 var networkProtocolNumber tcpip.NetworkProtocolNumber 3845 switch network { 3846 case "ipv4": 3847 networkProtocolNumber = ipv4.ProtocolNumber 3848 case "ipv6", "dual": 3849 networkProtocolNumber = ipv6.ProtocolNumber 3850 default: 3851 t.Fatalf("unknown network: '%s'", network) 3852 } 3853 ep, err := s.NewEndpoint(tcp.ProtocolNumber, networkProtocolNumber, &wq) 3854 if err != nil { 3855 t.Fatalf("NewEndpoint failed: %v", err) 3856 } 3857 eps = append(eps, ep) 3858 switch network { 3859 case "ipv4": 3860 case "ipv6": 3861 if err := ep.SetSockOpt(tcpip.V6OnlyOption(1)); err != nil { 3862 t.Fatalf("SetSockOpt(V6OnlyOption(1)) failed: %v", err) 3863 } 3864 case "dual": 3865 if err := ep.SetSockOpt(tcpip.V6OnlyOption(0)); err != nil { 3866 t.Fatalf("SetSockOpt(V6OnlyOption(0)) failed: %v", err) 3867 } 3868 default: 3869 t.Fatalf("unknown network: '%s'", network) 3870 } 3871 return ep 3872 } 3873 3874 var v4reserved, v6reserved bool 3875 switch exhaustedAddressType { 3876 case "v4", "mapped": 3877 v4reserved = true 3878 case "v6": 3879 v6reserved = true 3880 // Dual stack sockets bound to v6 any reserve on v4 as 3881 // well. 3882 if isAny { 3883 switch exhaustedNetwork { 3884 case "ipv6": 3885 case "dual": 3886 v4reserved = true 3887 default: 3888 t.Fatalf("unknown address type: '%s'", exhaustedNetwork) 3889 } 3890 } 3891 default: 3892 t.Fatalf("unknown address type: '%s'", exhaustedAddressType) 3893 } 3894 var collides bool 3895 switch candidateAddressType { 3896 case "v4", "mapped": 3897 collides = v4reserved 3898 case "v6": 3899 collides = v6reserved 3900 default: 3901 t.Fatalf("unknown address type: '%s'", candidateAddressType) 3902 } 3903 3904 for i := ports.FirstEphemeral; i <= math.MaxUint16; i++ { 3905 if makeEP(exhaustedNetwork).Bind(tcpip.FullAddress{Addr: address(t, exhaustedAddressType, isAny), Port: uint16(i)}); err != nil { 3906 t.Fatalf("Bind(%d) failed: %v", i, err) 3907 } 3908 } 3909 want := tcpip.ErrConnectStarted 3910 if collides { 3911 want = tcpip.ErrNoPortAvailable 3912 } 3913 if err := makeEP(candidateNetwork).Connect(tcpip.FullAddress{Addr: address(t, candidateAddressType, false), Port: 31337}); err != want { 3914 t.Fatalf("got ep.Connect(..) = %v, want = %v", err, want) 3915 } 3916 }) 3917 } 3918 }) 3919 } 3920 }) 3921 } 3922 }) 3923 } 3924 }) 3925 } 3926 } 3927 3928 func TestPathMTUDiscovery(t *testing.T) { 3929 // This test verifies the stack retransmits packets after it receives an 3930 // ICMP packet indicating that the path MTU has been exceeded. 3931 c := context.New(t, 1500) 3932 defer c.Cleanup() 3933 3934 // Create new connection with MSS of 1460. 3935 const maxPayload = 1500 - header.TCPMinimumSize - header.IPv4MinimumSize 3936 c.CreateConnectedWithRawOptions(789, 30000, -1 /* epRcvBuf */, []byte{ 3937 header.TCPOptionMSS, 4, byte(maxPayload / 256), byte(maxPayload % 256), 3938 }) 3939 3940 // Send 3200 bytes of data. 3941 const writeSize = 3200 3942 data := buffer.NewView(writeSize) 3943 for i := range data { 3944 data[i] = byte(i) 3945 } 3946 3947 if _, _, err := c.EP.Write(tcpip.SlicePayload(data), tcpip.WriteOptions{}); err != nil { 3948 t.Fatalf("Write failed: %v", err) 3949 } 3950 3951 receivePackets := func(c *context.Context, sizes []int, which int, seqNum uint32) []byte { 3952 var ret []byte 3953 for i, size := range sizes { 3954 p := c.GetPacket() 3955 if i == which { 3956 ret = p 3957 } 3958 checker.IPv4(t, p, 3959 checker.PayloadLen(size+header.TCPMinimumSize), 3960 checker.TCP( 3961 checker.DstPort(context.TestPort), 3962 checker.SeqNum(seqNum), 3963 checker.AckNum(790), 3964 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 3965 ), 3966 ) 3967 seqNum += uint32(size) 3968 } 3969 return ret 3970 } 3971 3972 // Receive three packets. 3973 sizes := []int{maxPayload, maxPayload, writeSize - 2*maxPayload} 3974 first := receivePackets(c, sizes, 0, uint32(c.IRS)+1) 3975 3976 // Send "packet too big" messages back to netstack. 3977 const newMTU = 1200 3978 const newMaxPayload = newMTU - header.IPv4MinimumSize - header.TCPMinimumSize 3979 mtu := []byte{0, 0, newMTU / 256, newMTU % 256} 3980 c.SendICMPPacket(header.ICMPv4DstUnreachable, header.ICMPv4FragmentationNeeded, mtu, first, newMTU) 3981 3982 // See retransmitted packets. None exceeding the new max. 3983 sizes = []int{newMaxPayload, maxPayload - newMaxPayload, newMaxPayload, maxPayload - newMaxPayload, writeSize - 2*maxPayload} 3984 receivePackets(c, sizes, -1, uint32(c.IRS)+1) 3985 } 3986 3987 func TestTCPEndpointProbe(t *testing.T) { 3988 c := context.New(t, 1500) 3989 defer c.Cleanup() 3990 3991 invoked := make(chan struct{}) 3992 c.Stack().AddTCPProbe(func(state stack.TCPEndpointState) { 3993 // Validate that the endpoint ID is what we expect. 3994 // 3995 // We don't do an extensive validation of every field but a 3996 // basic sanity test. 3997 if got, want := state.ID.LocalAddress, tcpip.Address(context.StackAddr); got != want { 3998 t.Fatalf("got LocalAddress: %q, want: %q", got, want) 3999 } 4000 if got, want := state.ID.LocalPort, c.Port; got != want { 4001 t.Fatalf("got LocalPort: %d, want: %d", got, want) 4002 } 4003 if got, want := state.ID.RemoteAddress, tcpip.Address(context.TestAddr); got != want { 4004 t.Fatalf("got RemoteAddress: %q, want: %q", got, want) 4005 } 4006 if got, want := state.ID.RemotePort, uint16(context.TestPort); got != want { 4007 t.Fatalf("got RemotePort: %d, want: %d", got, want) 4008 } 4009 4010 invoked <- struct{}{} 4011 }) 4012 4013 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 4014 4015 data := []byte{1, 2, 3} 4016 c.SendPacket(data, &context.Headers{ 4017 SrcPort: context.TestPort, 4018 DstPort: c.Port, 4019 Flags: header.TCPFlagAck, 4020 SeqNum: 790, 4021 AckNum: c.IRS.Add(1), 4022 RcvWnd: 30000, 4023 }) 4024 4025 select { 4026 case <-invoked: 4027 case <-time.After(100 * time.Millisecond): 4028 t.Fatalf("TCP Probe function was not called") 4029 } 4030 } 4031 4032 func TestStackSetCongestionControl(t *testing.T) { 4033 testCases := []struct { 4034 cc tcpip.CongestionControlOption 4035 err *tcpip.Error 4036 }{ 4037 {"reno", nil}, 4038 {"cubic", nil}, 4039 {"blahblah", tcpip.ErrNoSuchFile}, 4040 } 4041 4042 for _, tc := range testCases { 4043 t.Run(fmt.Sprintf("SetTransportProtocolOption(.., %v)", tc.cc), func(t *testing.T) { 4044 c := context.New(t, 1500) 4045 defer c.Cleanup() 4046 4047 s := c.Stack() 4048 4049 var oldCC tcpip.CongestionControlOption 4050 if err := s.TransportProtocolOption(tcp.ProtocolNumber, &oldCC); err != nil { 4051 t.Fatalf("s.TransportProtocolOption(%v, %v) = %v", tcp.ProtocolNumber, &oldCC, err) 4052 } 4053 4054 if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, tc.cc); err != tc.err { 4055 t.Fatalf("s.SetTransportProtocolOption(%v, %v) = %v, want %v", tcp.ProtocolNumber, tc.cc, err, tc.err) 4056 } 4057 4058 var cc tcpip.CongestionControlOption 4059 if err := s.TransportProtocolOption(tcp.ProtocolNumber, &cc); err != nil { 4060 t.Fatalf("s.TransportProtocolOption(%v, %v) = %v", tcp.ProtocolNumber, &cc, err) 4061 } 4062 4063 got, want := cc, oldCC 4064 // If SetTransportProtocolOption is expected to succeed 4065 // then the returned value for congestion control should 4066 // match the one specified in the 4067 // SetTransportProtocolOption call above, else it should 4068 // be what it was before the call to 4069 // SetTransportProtocolOption. 4070 if tc.err == nil { 4071 want = tc.cc 4072 } 4073 if got != want { 4074 t.Fatalf("got congestion control: %v, want: %v", got, want) 4075 } 4076 }) 4077 } 4078 } 4079 4080 func TestStackAvailableCongestionControl(t *testing.T) { 4081 c := context.New(t, 1500) 4082 defer c.Cleanup() 4083 4084 s := c.Stack() 4085 4086 // Query permitted congestion control algorithms. 4087 var aCC tcpip.AvailableCongestionControlOption 4088 if err := s.TransportProtocolOption(tcp.ProtocolNumber, &aCC); err != nil { 4089 t.Fatalf("s.TransportProtocolOption(%v, %v) = %v", tcp.ProtocolNumber, &aCC, err) 4090 } 4091 if got, want := aCC, tcpip.AvailableCongestionControlOption("reno cubic"); got != want { 4092 t.Fatalf("got tcpip.AvailableCongestionControlOption: %v, want: %v", got, want) 4093 } 4094 } 4095 4096 func TestStackSetAvailableCongestionControl(t *testing.T) { 4097 c := context.New(t, 1500) 4098 defer c.Cleanup() 4099 4100 s := c.Stack() 4101 4102 // Setting AvailableCongestionControlOption should fail. 4103 aCC := tcpip.AvailableCongestionControlOption("xyz") 4104 if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &aCC); err == nil { 4105 t.Fatalf("s.TransportProtocolOption(%v, %v) = nil, want non-nil", tcp.ProtocolNumber, &aCC) 4106 } 4107 4108 // Verify that we still get the expected list of congestion control options. 4109 var cc tcpip.AvailableCongestionControlOption 4110 if err := s.TransportProtocolOption(tcp.ProtocolNumber, &cc); err != nil { 4111 t.Fatalf("s.TransportProtocolOption(%v, %v) = %v", tcp.ProtocolNumber, &cc, err) 4112 } 4113 if got, want := cc, tcpip.AvailableCongestionControlOption("reno cubic"); got != want { 4114 t.Fatalf("got tcpip.AvailableCongestionControlOption: %v, want: %v", got, want) 4115 } 4116 } 4117 4118 func TestEndpointSetCongestionControl(t *testing.T) { 4119 testCases := []struct { 4120 cc tcpip.CongestionControlOption 4121 err *tcpip.Error 4122 }{ 4123 {"reno", nil}, 4124 {"cubic", nil}, 4125 {"blahblah", tcpip.ErrNoSuchFile}, 4126 } 4127 4128 for _, connected := range []bool{false, true} { 4129 for _, tc := range testCases { 4130 t.Run(fmt.Sprintf("SetSockOpt(.., %v) w/ connected = %v", tc.cc, connected), func(t *testing.T) { 4131 c := context.New(t, 1500) 4132 defer c.Cleanup() 4133 4134 // Create TCP endpoint. 4135 var err *tcpip.Error 4136 c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ) 4137 if err != nil { 4138 t.Fatalf("NewEndpoint failed: %v", err) 4139 } 4140 4141 var oldCC tcpip.CongestionControlOption 4142 if err := c.EP.GetSockOpt(&oldCC); err != nil { 4143 t.Fatalf("c.EP.SockOpt(%v) = %v", &oldCC, err) 4144 } 4145 4146 if connected { 4147 c.Connect(789 /* iss */, 32768 /* rcvWnd */, nil) 4148 } 4149 4150 if err := c.EP.SetSockOpt(tc.cc); err != tc.err { 4151 t.Fatalf("c.EP.SetSockOpt(%v) = %v, want %v", tc.cc, err, tc.err) 4152 } 4153 4154 var cc tcpip.CongestionControlOption 4155 if err := c.EP.GetSockOpt(&cc); err != nil { 4156 t.Fatalf("c.EP.SockOpt(%v) = %v", &cc, err) 4157 } 4158 4159 got, want := cc, oldCC 4160 // If SetSockOpt is expected to succeed then the 4161 // returned value for congestion control should match 4162 // the one specified in the SetSockOpt above, else it 4163 // should be what it was before the call to SetSockOpt. 4164 if tc.err == nil { 4165 want = tc.cc 4166 } 4167 if got != want { 4168 t.Fatalf("got congestion control: %v, want: %v", got, want) 4169 } 4170 }) 4171 } 4172 } 4173 } 4174 4175 func enableCUBIC(t *testing.T, c *context.Context) { 4176 t.Helper() 4177 opt := tcpip.CongestionControlOption("cubic") 4178 if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, opt); err != nil { 4179 t.Fatalf("c.s.SetTransportProtocolOption(tcp.ProtocolNumber, %v = %v", opt, err) 4180 } 4181 } 4182 4183 func TestKeepalive(t *testing.T) { 4184 c := context.New(t, defaultMTU) 4185 defer c.Cleanup() 4186 4187 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 4188 4189 c.EP.SetSockOpt(tcpip.KeepaliveIdleOption(10 * time.Millisecond)) 4190 c.EP.SetSockOpt(tcpip.KeepaliveIntervalOption(10 * time.Millisecond)) 4191 c.EP.SetSockOpt(tcpip.KeepaliveCountOption(5)) 4192 c.EP.SetSockOpt(tcpip.KeepaliveEnabledOption(1)) 4193 4194 // 5 unacked keepalives are sent. ACK each one, and check that the 4195 // connection stays alive after 5. 4196 for i := 0; i < 10; i++ { 4197 b := c.GetPacket() 4198 checker.IPv4(t, b, 4199 checker.TCP( 4200 checker.DstPort(context.TestPort), 4201 checker.SeqNum(uint32(c.IRS)), 4202 checker.AckNum(uint32(790)), 4203 checker.TCPFlags(header.TCPFlagAck), 4204 ), 4205 ) 4206 4207 // Acknowledge the keepalive. 4208 c.SendPacket(nil, &context.Headers{ 4209 SrcPort: context.TestPort, 4210 DstPort: c.Port, 4211 Flags: header.TCPFlagAck, 4212 SeqNum: 790, 4213 AckNum: c.IRS, 4214 RcvWnd: 30000, 4215 }) 4216 } 4217 4218 // Check that the connection is still alive. 4219 if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock { 4220 t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock) 4221 } 4222 4223 // Send some data and wait before ACKing it. Keepalives should be disabled 4224 // during this period. 4225 view := buffer.NewView(3) 4226 if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil { 4227 t.Fatalf("Write failed: %v", err) 4228 } 4229 4230 next := uint32(c.IRS) + 1 4231 checker.IPv4(t, c.GetPacket(), 4232 checker.PayloadLen(len(view)+header.TCPMinimumSize), 4233 checker.TCP( 4234 checker.DstPort(context.TestPort), 4235 checker.SeqNum(next), 4236 checker.AckNum(790), 4237 checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)), 4238 ), 4239 ) 4240 4241 // Wait for the packet to be retransmitted. Verify that no keepalives 4242 // were sent. 4243 checker.IPv4(t, c.GetPacket(), 4244 checker.PayloadLen(len(view)+header.TCPMinimumSize), 4245 checker.TCP( 4246 checker.DstPort(context.TestPort), 4247 checker.SeqNum(next), 4248 checker.AckNum(790), 4249 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagPsh), 4250 ), 4251 ) 4252 c.CheckNoPacket("Keepalive packet received while unACKed data is pending") 4253 4254 next += uint32(len(view)) 4255 4256 // Send ACK. Keepalives should start sending again. 4257 c.SendPacket(nil, &context.Headers{ 4258 SrcPort: context.TestPort, 4259 DstPort: c.Port, 4260 Flags: header.TCPFlagAck, 4261 SeqNum: 790, 4262 AckNum: seqnum.Value(next), 4263 RcvWnd: 30000, 4264 }) 4265 4266 // Now receive 5 keepalives, but don't ACK them. The connection 4267 // should be reset after 5. 4268 for i := 0; i < 5; i++ { 4269 b := c.GetPacket() 4270 checker.IPv4(t, b, 4271 checker.TCP( 4272 checker.DstPort(context.TestPort), 4273 checker.SeqNum(uint32(next-1)), 4274 checker.AckNum(uint32(790)), 4275 checker.TCPFlags(header.TCPFlagAck), 4276 ), 4277 ) 4278 } 4279 4280 // The connection should be terminated after 5 unacked keepalives. 4281 checker.IPv4(t, c.GetPacket(), 4282 checker.TCP( 4283 checker.DstPort(context.TestPort), 4284 checker.SeqNum(uint32(next)), 4285 checker.AckNum(uint32(790)), 4286 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagRst), 4287 ), 4288 ) 4289 4290 if _, _, err := c.EP.Read(nil); err != tcpip.ErrTimeout { 4291 t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrTimeout) 4292 } 4293 } 4294 4295 func executeHandshake(t *testing.T, c *context.Context, srcPort uint16, synCookieInUse bool) (irs, iss seqnum.Value) { 4296 // Send a SYN request. 4297 irs = seqnum.Value(789) 4298 c.SendPacket(nil, &context.Headers{ 4299 SrcPort: srcPort, 4300 DstPort: context.StackPort, 4301 Flags: header.TCPFlagSyn, 4302 SeqNum: irs, 4303 RcvWnd: 30000, 4304 }) 4305 4306 // Receive the SYN-ACK reply.w 4307 b := c.GetPacket() 4308 tcp := header.TCP(header.IPv4(b).Payload()) 4309 iss = seqnum.Value(tcp.SequenceNumber()) 4310 tcpCheckers := []checker.TransportChecker{ 4311 checker.SrcPort(context.StackPort), 4312 checker.DstPort(srcPort), 4313 checker.TCPFlags(header.TCPFlagAck | header.TCPFlagSyn), 4314 checker.AckNum(uint32(irs) + 1), 4315 } 4316 4317 if synCookieInUse { 4318 // When cookies are in use window scaling is disabled. 4319 tcpCheckers = append(tcpCheckers, checker.TCPSynOptions(header.TCPSynOptions{ 4320 WS: -1, 4321 MSS: c.MSSWithoutOptions(), 4322 })) 4323 } 4324 4325 checker.IPv4(t, b, checker.TCP(tcpCheckers...)) 4326 4327 // Send ACK. 4328 c.SendPacket(nil, &context.Headers{ 4329 SrcPort: srcPort, 4330 DstPort: context.StackPort, 4331 Flags: header.TCPFlagAck, 4332 SeqNum: irs + 1, 4333 AckNum: iss + 1, 4334 RcvWnd: 30000, 4335 }) 4336 return irs, iss 4337 } 4338 4339 // TestListenBacklogFull tests that netstack does not complete handshakes if the 4340 // listen backlog for the endpoint is full. 4341 func TestListenBacklogFull(t *testing.T) { 4342 c := context.New(t, defaultMTU) 4343 defer c.Cleanup() 4344 4345 // Create TCP endpoint. 4346 var err *tcpip.Error 4347 c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ) 4348 if err != nil { 4349 t.Fatalf("NewEndpoint failed: %v", err) 4350 } 4351 4352 // Bind to wildcard. 4353 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 4354 t.Fatalf("Bind failed: %v", err) 4355 } 4356 4357 // Test acceptance. 4358 // Start listening. 4359 listenBacklog := 2 4360 if err := c.EP.Listen(listenBacklog); err != nil { 4361 t.Fatalf("Listen failed: %v", err) 4362 } 4363 4364 for i := 0; i < listenBacklog; i++ { 4365 executeHandshake(t, c, context.TestPort+uint16(i), false /*synCookieInUse */) 4366 } 4367 4368 time.Sleep(50 * time.Millisecond) 4369 4370 // Now execute send one more SYN. The stack should not respond as the backlog 4371 // is full at this point. 4372 c.SendPacket(nil, &context.Headers{ 4373 SrcPort: context.TestPort + 2, 4374 DstPort: context.StackPort, 4375 Flags: header.TCPFlagSyn, 4376 SeqNum: seqnum.Value(789), 4377 RcvWnd: 30000, 4378 }) 4379 c.CheckNoPacketTimeout("unexpected packet received", 50*time.Millisecond) 4380 4381 // Try to accept the connections in the backlog. 4382 we, ch := waiter.NewChannelEntry(nil) 4383 c.WQ.EventRegister(&we, waiter.EventIn) 4384 defer c.WQ.EventUnregister(&we) 4385 4386 for i := 0; i < listenBacklog; i++ { 4387 _, _, err = c.EP.Accept() 4388 if err == tcpip.ErrWouldBlock { 4389 // Wait for connection to be established. 4390 select { 4391 case <-ch: 4392 _, _, err = c.EP.Accept() 4393 if err != nil { 4394 t.Fatalf("Accept failed: %v", err) 4395 } 4396 4397 case <-time.After(1 * time.Second): 4398 t.Fatalf("Timed out waiting for accept") 4399 } 4400 } 4401 } 4402 4403 // Now verify that there are no more connections that can be accepted. 4404 _, _, err = c.EP.Accept() 4405 if err != tcpip.ErrWouldBlock { 4406 select { 4407 case <-ch: 4408 t.Fatalf("unexpected endpoint delivered on Accept: %+v", c.EP) 4409 case <-time.After(1 * time.Second): 4410 } 4411 } 4412 4413 // Now a new handshake must succeed. 4414 executeHandshake(t, c, context.TestPort+2, false /*synCookieInUse */) 4415 4416 newEP, _, err := c.EP.Accept() 4417 if err == tcpip.ErrWouldBlock { 4418 // Wait for connection to be established. 4419 select { 4420 case <-ch: 4421 newEP, _, err = c.EP.Accept() 4422 if err != nil { 4423 t.Fatalf("Accept failed: %v", err) 4424 } 4425 4426 case <-time.After(1 * time.Second): 4427 t.Fatalf("Timed out waiting for accept") 4428 } 4429 } 4430 4431 // Now verify that the TCP socket is usable and in a connected state. 4432 data := "Don't panic" 4433 newEP.Write(tcpip.SlicePayload(buffer.NewViewFromBytes([]byte(data))), tcpip.WriteOptions{}) 4434 b := c.GetPacket() 4435 tcp := header.TCP(header.IPv4(b).Payload()) 4436 if string(tcp.Payload()) != data { 4437 t.Fatalf("Unexpected data: got %v, want %v", string(tcp.Payload()), data) 4438 } 4439 } 4440 4441 // TestListenNoAcceptMulticastBroadcastV4 makes sure that TCP segments with a 4442 // non unicast IPv4 address are not accepted. 4443 func TestListenNoAcceptNonUnicastV4(t *testing.T) { 4444 multicastAddr := tcpip.Address("\xe0\x00\x01\x02") 4445 otherMulticastAddr := tcpip.Address("\xe0\x00\x01\x03") 4446 4447 tests := []struct { 4448 name string 4449 srcAddr tcpip.Address 4450 dstAddr tcpip.Address 4451 }{ 4452 { 4453 "SourceUnspecified", 4454 header.IPv4Any, 4455 context.StackAddr, 4456 }, 4457 { 4458 "SourceBroadcast", 4459 header.IPv4Broadcast, 4460 context.StackAddr, 4461 }, 4462 { 4463 "SourceOurMulticast", 4464 multicastAddr, 4465 context.StackAddr, 4466 }, 4467 { 4468 "SourceOtherMulticast", 4469 otherMulticastAddr, 4470 context.StackAddr, 4471 }, 4472 { 4473 "DestUnspecified", 4474 context.TestAddr, 4475 header.IPv4Any, 4476 }, 4477 { 4478 "DestBroadcast", 4479 context.TestAddr, 4480 header.IPv4Broadcast, 4481 }, 4482 { 4483 "DestOurMulticast", 4484 context.TestAddr, 4485 multicastAddr, 4486 }, 4487 { 4488 "DestOtherMulticast", 4489 context.TestAddr, 4490 otherMulticastAddr, 4491 }, 4492 } 4493 4494 for _, test := range tests { 4495 t.Run(test.name, func(t *testing.T) { 4496 t.Parallel() 4497 4498 c := context.New(t, defaultMTU) 4499 defer c.Cleanup() 4500 4501 c.Create(-1) 4502 4503 if err := c.Stack().JoinGroup(header.IPv4ProtocolNumber, 1, multicastAddr); err != nil { 4504 t.Fatalf("JoinGroup failed: %s", err) 4505 } 4506 4507 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 4508 t.Fatalf("Bind failed: %s", err) 4509 } 4510 4511 if err := c.EP.Listen(1); err != nil { 4512 t.Fatalf("Listen failed: %s", err) 4513 } 4514 4515 irs := seqnum.Value(789) 4516 c.SendPacketWithAddrs(nil, &context.Headers{ 4517 SrcPort: context.TestPort, 4518 DstPort: context.StackPort, 4519 Flags: header.TCPFlagSyn, 4520 SeqNum: irs, 4521 RcvWnd: 30000, 4522 }, test.srcAddr, test.dstAddr) 4523 c.CheckNoPacket("Should not have received a response") 4524 4525 // Handle normal packet. 4526 c.SendPacketWithAddrs(nil, &context.Headers{ 4527 SrcPort: context.TestPort, 4528 DstPort: context.StackPort, 4529 Flags: header.TCPFlagSyn, 4530 SeqNum: irs, 4531 RcvWnd: 30000, 4532 }, context.TestAddr, context.StackAddr) 4533 checker.IPv4(t, c.GetPacket(), 4534 checker.TCP( 4535 checker.SrcPort(context.StackPort), 4536 checker.DstPort(context.TestPort), 4537 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagSyn), 4538 checker.AckNum(uint32(irs)+1))) 4539 }) 4540 } 4541 } 4542 4543 // TestListenNoAcceptMulticastBroadcastV6 makes sure that TCP segments with a 4544 // non unicast IPv6 address are not accepted. 4545 func TestListenNoAcceptNonUnicastV6(t *testing.T) { 4546 multicastAddr := tcpip.Address("\xff\x0e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01") 4547 otherMulticastAddr := tcpip.Address("\xff\x0e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02") 4548 4549 tests := []struct { 4550 name string 4551 srcAddr tcpip.Address 4552 dstAddr tcpip.Address 4553 }{ 4554 { 4555 "SourceUnspecified", 4556 header.IPv6Any, 4557 context.StackV6Addr, 4558 }, 4559 { 4560 "SourceAllNodes", 4561 header.IPv6AllNodesMulticastAddress, 4562 context.StackV6Addr, 4563 }, 4564 { 4565 "SourceOurMulticast", 4566 multicastAddr, 4567 context.StackV6Addr, 4568 }, 4569 { 4570 "SourceOtherMulticast", 4571 otherMulticastAddr, 4572 context.StackV6Addr, 4573 }, 4574 { 4575 "DestUnspecified", 4576 context.TestV6Addr, 4577 header.IPv6Any, 4578 }, 4579 { 4580 "DestAllNodes", 4581 context.TestV6Addr, 4582 header.IPv6AllNodesMulticastAddress, 4583 }, 4584 { 4585 "DestOurMulticast", 4586 context.TestV6Addr, 4587 multicastAddr, 4588 }, 4589 { 4590 "DestOtherMulticast", 4591 context.TestV6Addr, 4592 otherMulticastAddr, 4593 }, 4594 } 4595 4596 for _, test := range tests { 4597 t.Run(test.name, func(t *testing.T) { 4598 t.Parallel() 4599 4600 c := context.New(t, defaultMTU) 4601 defer c.Cleanup() 4602 4603 c.CreateV6Endpoint(true) 4604 4605 if err := c.Stack().JoinGroup(header.IPv6ProtocolNumber, 1, multicastAddr); err != nil { 4606 t.Fatalf("JoinGroup failed: %s", err) 4607 } 4608 4609 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 4610 t.Fatalf("Bind failed: %s", err) 4611 } 4612 4613 if err := c.EP.Listen(1); err != nil { 4614 t.Fatalf("Listen failed: %s", err) 4615 } 4616 4617 irs := seqnum.Value(789) 4618 c.SendV6PacketWithAddrs(nil, &context.Headers{ 4619 SrcPort: context.TestPort, 4620 DstPort: context.StackPort, 4621 Flags: header.TCPFlagSyn, 4622 SeqNum: irs, 4623 RcvWnd: 30000, 4624 }, test.srcAddr, test.dstAddr) 4625 c.CheckNoPacket("Should not have received a response") 4626 4627 // Handle normal packet. 4628 c.SendV6PacketWithAddrs(nil, &context.Headers{ 4629 SrcPort: context.TestPort, 4630 DstPort: context.StackPort, 4631 Flags: header.TCPFlagSyn, 4632 SeqNum: irs, 4633 RcvWnd: 30000, 4634 }, context.TestV6Addr, context.StackV6Addr) 4635 checker.IPv6(t, c.GetV6Packet(), 4636 checker.TCP( 4637 checker.SrcPort(context.StackPort), 4638 checker.DstPort(context.TestPort), 4639 checker.TCPFlags(header.TCPFlagAck|header.TCPFlagSyn), 4640 checker.AckNum(uint32(irs)+1))) 4641 }) 4642 } 4643 } 4644 4645 func TestListenSynRcvdQueueFull(t *testing.T) { 4646 c := context.New(t, defaultMTU) 4647 defer c.Cleanup() 4648 4649 // Create TCP endpoint. 4650 var err *tcpip.Error 4651 c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ) 4652 if err != nil { 4653 t.Fatalf("NewEndpoint failed: %v", err) 4654 } 4655 4656 // Bind to wildcard. 4657 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 4658 t.Fatalf("Bind failed: %v", err) 4659 } 4660 4661 // Test acceptance. 4662 // Start listening. 4663 listenBacklog := 1 4664 if err := c.EP.Listen(listenBacklog); err != nil { 4665 t.Fatalf("Listen failed: %v", err) 4666 } 4667 4668 // Send two SYN's the first one should get a SYN-ACK, the 4669 // second one should not get any response and is dropped as 4670 // the synRcvd count will be equal to backlog. 4671 irs := seqnum.Value(789) 4672 c.SendPacket(nil, &context.Headers{ 4673 SrcPort: context.TestPort, 4674 DstPort: context.StackPort, 4675 Flags: header.TCPFlagSyn, 4676 SeqNum: seqnum.Value(789), 4677 RcvWnd: 30000, 4678 }) 4679 4680 // Receive the SYN-ACK reply. 4681 b := c.GetPacket() 4682 tcp := header.TCP(header.IPv4(b).Payload()) 4683 iss := seqnum.Value(tcp.SequenceNumber()) 4684 tcpCheckers := []checker.TransportChecker{ 4685 checker.SrcPort(context.StackPort), 4686 checker.DstPort(context.TestPort), 4687 checker.TCPFlags(header.TCPFlagAck | header.TCPFlagSyn), 4688 checker.AckNum(uint32(irs) + 1), 4689 } 4690 checker.IPv4(t, b, checker.TCP(tcpCheckers...)) 4691 4692 // Now execute send one more SYN. The stack should not respond as the backlog 4693 // is full at this point. 4694 // 4695 // NOTE: we did not complete the handshake for the previous one so the 4696 // accept backlog should be empty and there should be one connection in 4697 // synRcvd state. 4698 c.SendPacket(nil, &context.Headers{ 4699 SrcPort: context.TestPort + 1, 4700 DstPort: context.StackPort, 4701 Flags: header.TCPFlagSyn, 4702 SeqNum: seqnum.Value(889), 4703 RcvWnd: 30000, 4704 }) 4705 c.CheckNoPacketTimeout("unexpected packet received", 50*time.Millisecond) 4706 4707 // Now complete the previous connection and verify that there is a connection 4708 // to accept. 4709 // Send ACK. 4710 c.SendPacket(nil, &context.Headers{ 4711 SrcPort: context.TestPort, 4712 DstPort: context.StackPort, 4713 Flags: header.TCPFlagAck, 4714 SeqNum: irs + 1, 4715 AckNum: iss + 1, 4716 RcvWnd: 30000, 4717 }) 4718 4719 // Try to accept the connections in the backlog. 4720 we, ch := waiter.NewChannelEntry(nil) 4721 c.WQ.EventRegister(&we, waiter.EventIn) 4722 defer c.WQ.EventUnregister(&we) 4723 4724 newEP, _, err := c.EP.Accept() 4725 if err == tcpip.ErrWouldBlock { 4726 // Wait for connection to be established. 4727 select { 4728 case <-ch: 4729 newEP, _, err = c.EP.Accept() 4730 if err != nil { 4731 t.Fatalf("Accept failed: %v", err) 4732 } 4733 4734 case <-time.After(1 * time.Second): 4735 t.Fatalf("Timed out waiting for accept") 4736 } 4737 } 4738 4739 // Now verify that the TCP socket is usable and in a connected state. 4740 data := "Don't panic" 4741 newEP.Write(tcpip.SlicePayload(buffer.NewViewFromBytes([]byte(data))), tcpip.WriteOptions{}) 4742 pkt := c.GetPacket() 4743 tcp = header.TCP(header.IPv4(pkt).Payload()) 4744 if string(tcp.Payload()) != data { 4745 t.Fatalf("Unexpected data: got %v, want %v", string(tcp.Payload()), data) 4746 } 4747 } 4748 4749 func TestListenBacklogFullSynCookieInUse(t *testing.T) { 4750 saved := tcp.SynRcvdCountThreshold 4751 defer func() { 4752 tcp.SynRcvdCountThreshold = saved 4753 }() 4754 tcp.SynRcvdCountThreshold = 1 4755 4756 c := context.New(t, defaultMTU) 4757 defer c.Cleanup() 4758 4759 // Create TCP endpoint. 4760 var err *tcpip.Error 4761 c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ) 4762 if err != nil { 4763 t.Fatalf("NewEndpoint failed: %v", err) 4764 } 4765 4766 // Bind to wildcard. 4767 if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 4768 t.Fatalf("Bind failed: %v", err) 4769 } 4770 4771 // Test acceptance. 4772 // Start listening. 4773 listenBacklog := 1 4774 portOffset := uint16(0) 4775 if err := c.EP.Listen(listenBacklog); err != nil { 4776 t.Fatalf("Listen failed: %v", err) 4777 } 4778 4779 executeHandshake(t, c, context.TestPort+portOffset, false) 4780 portOffset++ 4781 // Wait for this to be delivered to the accept queue. 4782 time.Sleep(50 * time.Millisecond) 4783 4784 // Send a SYN request. 4785 irs := seqnum.Value(789) 4786 c.SendPacket(nil, &context.Headers{ 4787 // pick a different src port for new SYN. 4788 SrcPort: context.TestPort + 1, 4789 DstPort: context.StackPort, 4790 Flags: header.TCPFlagSyn, 4791 SeqNum: irs, 4792 RcvWnd: 30000, 4793 }) 4794 // The Syn should be dropped as the endpoint's backlog is full. 4795 c.CheckNoPacketTimeout("unexpected packet received", 50*time.Millisecond) 4796 4797 // Verify that there is only one acceptable connection at this point. 4798 we, ch := waiter.NewChannelEntry(nil) 4799 c.WQ.EventRegister(&we, waiter.EventIn) 4800 defer c.WQ.EventUnregister(&we) 4801 4802 _, _, err = c.EP.Accept() 4803 if err == tcpip.ErrWouldBlock { 4804 // Wait for connection to be established. 4805 select { 4806 case <-ch: 4807 _, _, err = c.EP.Accept() 4808 if err != nil { 4809 t.Fatalf("Accept failed: %v", err) 4810 } 4811 4812 case <-time.After(1 * time.Second): 4813 t.Fatalf("Timed out waiting for accept") 4814 } 4815 } 4816 4817 // Now verify that there are no more connections that can be accepted. 4818 _, _, err = c.EP.Accept() 4819 if err != tcpip.ErrWouldBlock { 4820 select { 4821 case <-ch: 4822 t.Fatalf("unexpected endpoint delivered on Accept: %+v", c.EP) 4823 case <-time.After(1 * time.Second): 4824 } 4825 } 4826 } 4827 4828 func TestPassiveConnectionAttemptIncrement(t *testing.T) { 4829 c := context.New(t, defaultMTU) 4830 defer c.Cleanup() 4831 4832 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ) 4833 if err != nil { 4834 t.Fatalf("NewEndpoint failed: %v", err) 4835 } 4836 c.EP = ep 4837 if err := ep.Bind(tcpip.FullAddress{Addr: context.StackAddr, Port: context.StackPort}); err != nil { 4838 t.Fatalf("Bind failed: %v", err) 4839 } 4840 if got, want := tcp.EndpointState(ep.State()), tcp.StateBound; got != want { 4841 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 4842 } 4843 if err := c.EP.Listen(1); err != nil { 4844 t.Fatalf("Listen failed: %v", err) 4845 } 4846 if got, want := tcp.EndpointState(c.EP.State()), tcp.StateListen; got != want { 4847 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 4848 } 4849 4850 stats := c.Stack().Stats() 4851 want := stats.TCP.PassiveConnectionOpenings.Value() + 1 4852 4853 srcPort := uint16(context.TestPort) 4854 executeHandshake(t, c, srcPort+1, false) 4855 4856 we, ch := waiter.NewChannelEntry(nil) 4857 c.WQ.EventRegister(&we, waiter.EventIn) 4858 defer c.WQ.EventUnregister(&we) 4859 4860 // Verify that there is only one acceptable connection at this point. 4861 _, _, err = c.EP.Accept() 4862 if err == tcpip.ErrWouldBlock { 4863 // Wait for connection to be established. 4864 select { 4865 case <-ch: 4866 _, _, err = c.EP.Accept() 4867 if err != nil { 4868 t.Fatalf("Accept failed: %v", err) 4869 } 4870 4871 case <-time.After(1 * time.Second): 4872 t.Fatalf("Timed out waiting for accept") 4873 } 4874 } 4875 4876 if got := stats.TCP.PassiveConnectionOpenings.Value(); got != want { 4877 t.Errorf("got stats.TCP.PassiveConnectionOpenings.Value() = %v, want = %v", got, want) 4878 } 4879 } 4880 4881 func TestPassiveFailedConnectionAttemptIncrement(t *testing.T) { 4882 c := context.New(t, defaultMTU) 4883 defer c.Cleanup() 4884 4885 stats := c.Stack().Stats() 4886 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ) 4887 if err != nil { 4888 t.Fatalf("NewEndpoint failed: %v", err) 4889 } 4890 c.EP = ep 4891 if err := c.EP.Bind(tcpip.FullAddress{Addr: context.StackAddr, Port: context.StackPort}); err != nil { 4892 t.Fatalf("Bind failed: %v", err) 4893 } 4894 if err := c.EP.Listen(1); err != nil { 4895 t.Fatalf("Listen failed: %v", err) 4896 } 4897 4898 srcPort := uint16(context.TestPort) 4899 // Now attempt a handshakes it will fill up the accept backlog. 4900 executeHandshake(t, c, srcPort, false) 4901 4902 // Give time for the final ACK to be processed as otherwise the next handshake could 4903 // get accepted before the previous one based on goroutine scheduling. 4904 time.Sleep(50 * time.Millisecond) 4905 4906 want := stats.TCP.ListenOverflowSynDrop.Value() + 1 4907 4908 // Now we will send one more SYN and this one should get dropped 4909 // Send a SYN request. 4910 c.SendPacket(nil, &context.Headers{ 4911 SrcPort: srcPort + 2, 4912 DstPort: context.StackPort, 4913 Flags: header.TCPFlagSyn, 4914 SeqNum: seqnum.Value(789), 4915 RcvWnd: 30000, 4916 }) 4917 4918 time.Sleep(50 * time.Millisecond) 4919 if got := stats.TCP.ListenOverflowSynDrop.Value(); got != want { 4920 t.Errorf("got stats.TCP.ListenOverflowSynDrop.Value() = %v, want = %v", got, want) 4921 } 4922 if got := c.EP.Stats().(*tcp.Stats).ReceiveErrors.ListenOverflowSynDrop.Value(); got != want { 4923 t.Errorf("got EP stats Stats.ReceiveErrors.ListenOverflowSynDrop = %v, want = %v", got, want) 4924 } 4925 4926 we, ch := waiter.NewChannelEntry(nil) 4927 c.WQ.EventRegister(&we, waiter.EventIn) 4928 defer c.WQ.EventUnregister(&we) 4929 4930 // Now check that there is one acceptable connections. 4931 _, _, err = c.EP.Accept() 4932 if err == tcpip.ErrWouldBlock { 4933 // Wait for connection to be established. 4934 select { 4935 case <-ch: 4936 _, _, err = c.EP.Accept() 4937 if err != nil { 4938 t.Fatalf("Accept failed: %v", err) 4939 } 4940 4941 case <-time.After(1 * time.Second): 4942 t.Fatalf("Timed out waiting for accept") 4943 } 4944 } 4945 } 4946 4947 func TestEndpointBindListenAcceptState(t *testing.T) { 4948 c := context.New(t, defaultMTU) 4949 defer c.Cleanup() 4950 wq := &waiter.Queue{} 4951 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq) 4952 if err != nil { 4953 t.Fatalf("NewEndpoint failed: %v", err) 4954 } 4955 4956 if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 4957 t.Fatalf("Bind failed: %v", err) 4958 } 4959 if got, want := tcp.EndpointState(ep.State()), tcp.StateBound; got != want { 4960 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 4961 } 4962 4963 // Expect InvalidEndpointState errors on a read at this point. 4964 if _, _, err := ep.Read(nil); err != tcpip.ErrInvalidEndpointState { 4965 t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrInvalidEndpointState) 4966 } 4967 if got := ep.Stats().(*tcp.Stats).ReadErrors.InvalidEndpointState.Value(); got != 1 { 4968 t.Fatalf("got EP stats Stats.ReadErrors.InvalidEndpointState got %v want %v", got, 1) 4969 } 4970 4971 if err := ep.Listen(10); err != nil { 4972 t.Fatalf("Listen failed: %v", err) 4973 } 4974 if got, want := tcp.EndpointState(ep.State()), tcp.StateListen; got != want { 4975 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 4976 } 4977 4978 c.PassiveConnectWithOptions(100, 5, header.TCPSynOptions{MSS: defaultIPv4MSS}) 4979 4980 // Try to accept the connection. 4981 we, ch := waiter.NewChannelEntry(nil) 4982 wq.EventRegister(&we, waiter.EventIn) 4983 defer wq.EventUnregister(&we) 4984 4985 aep, _, err := ep.Accept() 4986 if err == tcpip.ErrWouldBlock { 4987 // Wait for connection to be established. 4988 select { 4989 case <-ch: 4990 aep, _, err = ep.Accept() 4991 if err != nil { 4992 t.Fatalf("Accept failed: %v", err) 4993 } 4994 4995 case <-time.After(1 * time.Second): 4996 t.Fatalf("Timed out waiting for accept") 4997 } 4998 } 4999 if got, want := tcp.EndpointState(aep.State()), tcp.StateEstablished; got != want { 5000 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 5001 } 5002 if err := aep.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrAlreadyConnected { 5003 t.Errorf("Unexpected error attempting to call connect on an established endpoint, got: %v, want: %v", err, tcpip.ErrAlreadyConnected) 5004 } 5005 // Listening endpoint remains in listen state. 5006 if got, want := tcp.EndpointState(ep.State()), tcp.StateListen; got != want { 5007 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 5008 } 5009 5010 ep.Close() 5011 // Give worker goroutines time to receive the close notification. 5012 time.Sleep(1 * time.Second) 5013 if got, want := tcp.EndpointState(ep.State()), tcp.StateClose; got != want { 5014 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 5015 } 5016 // Accepted endpoint remains open when the listen endpoint is closed. 5017 if got, want := tcp.EndpointState(aep.State()), tcp.StateEstablished; got != want { 5018 t.Errorf("Unexpected endpoint state: want %v, got %v", want, got) 5019 } 5020 5021 } 5022 5023 // This test verifies that the auto tuning does not grow the receive buffer if 5024 // the application is not reading the data actively. 5025 func TestReceiveBufferAutoTuningApplicationLimited(t *testing.T) { 5026 const mtu = 1500 5027 const mss = mtu - header.IPv4MinimumSize - header.TCPMinimumSize 5028 5029 c := context.New(t, mtu) 5030 defer c.Cleanup() 5031 5032 stk := c.Stack() 5033 // Set lower limits for auto-tuning tests. This is required because the 5034 // test stops the worker which can cause packets to be dropped because 5035 // the segment queue holding unprocessed packets is limited to 500. 5036 const receiveBufferSize = 80 << 10 // 80KB. 5037 const maxReceiveBufferSize = receiveBufferSize * 10 5038 if err := stk.SetTransportProtocolOption(tcp.ProtocolNumber, tcp.ReceiveBufferSizeOption{1, receiveBufferSize, maxReceiveBufferSize}); err != nil { 5039 t.Fatalf("SetTransportProtocolOption failed: %v", err) 5040 } 5041 5042 // Enable auto-tuning. 5043 if err := stk.SetTransportProtocolOption(tcp.ProtocolNumber, tcpip.ModerateReceiveBufferOption(true)); err != nil { 5044 t.Fatalf("SetTransportProtocolOption failed: %v", err) 5045 } 5046 // Change the expected window scale to match the value needed for the 5047 // maximum buffer size defined above. 5048 c.WindowScale = uint8(tcp.FindWndScale(maxReceiveBufferSize)) 5049 5050 rawEP := c.CreateConnectedWithOptions(header.TCPSynOptions{TS: true, WS: 4}) 5051 5052 // NOTE: The timestamp values in the sent packets are meaningless to the 5053 // peer so we just increment the timestamp value by 1 every batch as we 5054 // are not really using them for anything. Send a single byte to verify 5055 // the advertised window. 5056 tsVal := rawEP.TSVal + 1 5057 5058 // Introduce a 25ms latency by delaying the first byte. 5059 latency := 25 * time.Millisecond 5060 time.Sleep(latency) 5061 rawEP.SendPacketWithTS([]byte{1}, tsVal) 5062 5063 // Verify that the ACK has the expected window. 5064 wantRcvWnd := receiveBufferSize 5065 wantRcvWnd = (wantRcvWnd >> uint32(c.WindowScale)) 5066 rawEP.VerifyACKRcvWnd(uint16(wantRcvWnd - 1)) 5067 time.Sleep(25 * time.Millisecond) 5068 5069 // Allocate a large enough payload for the test. 5070 b := make([]byte, int(receiveBufferSize)*2) 5071 offset := 0 5072 payloadSize := receiveBufferSize - 1 5073 worker := (c.EP).(interface { 5074 StopWork() 5075 ResumeWork() 5076 }) 5077 tsVal++ 5078 5079 // Stop the worker goroutine. 5080 worker.StopWork() 5081 start := offset 5082 end := offset + payloadSize 5083 packetsSent := 0 5084 for ; start < end; start += mss { 5085 rawEP.SendPacketWithTS(b[start:start+mss], tsVal) 5086 packetsSent++ 5087 } 5088 // Resume the worker so that it only sees the packets once all of them 5089 // are waiting to be read. 5090 worker.ResumeWork() 5091 5092 // Since we read no bytes the window should goto zero till the 5093 // application reads some of the data. 5094 // Discard all intermediate acks except the last one. 5095 if packetsSent > 100 { 5096 for i := 0; i < (packetsSent / 100); i++ { 5097 _ = c.GetPacket() 5098 } 5099 } 5100 rawEP.VerifyACKRcvWnd(0) 5101 5102 time.Sleep(25 * time.Millisecond) 5103 // Verify that sending more data when window is closed is dropped and 5104 // not acked. 5105 rawEP.SendPacketWithTS(b[start:start+mss], tsVal) 5106 5107 // Verify that the stack sends us back an ACK with the sequence number 5108 // of the last packet sent indicating it was dropped. 5109 p := c.GetPacket() 5110 checker.IPv4(t, p, checker.TCP( 5111 checker.AckNum(uint32(rawEP.NextSeqNum)-uint32(mss)), 5112 checker.Window(0), 5113 )) 5114 5115 // Now read all the data from the endpoint and verify that advertised 5116 // window increases to the full available buffer size. 5117 for { 5118 _, _, err := c.EP.Read(nil) 5119 if err == tcpip.ErrWouldBlock { 5120 break 5121 } 5122 } 5123 5124 // Verify that we receive a non-zero window update ACK. When running 5125 // under thread santizer this test can end up sending more than 1 5126 // ack, 1 for the non-zero window 5127 p = c.GetPacket() 5128 checker.IPv4(t, p, checker.TCP( 5129 checker.AckNum(uint32(rawEP.NextSeqNum)-uint32(mss)), 5130 func(t *testing.T, h header.Transport) { 5131 tcp, ok := h.(header.TCP) 5132 if !ok { 5133 return 5134 } 5135 if w := tcp.WindowSize(); w == 0 || w > uint16(wantRcvWnd) { 5136 t.Errorf("expected a non-zero window: got %d, want <= wantRcvWnd", w, wantRcvWnd) 5137 } 5138 }, 5139 )) 5140 } 5141 5142 // This test verifies that the auto tuning does not grow the receive buffer if 5143 // the application is not reading the data actively. 5144 func TestReceiveBufferAutoTuning(t *testing.T) { 5145 const mtu = 1500 5146 const mss = mtu - header.IPv4MinimumSize - header.TCPMinimumSize 5147 5148 c := context.New(t, mtu) 5149 defer c.Cleanup() 5150 5151 // Enable Auto-tuning. 5152 stk := c.Stack() 5153 // Set lower limits for auto-tuning tests. This is required because the 5154 // test stops the worker which can cause packets to be dropped because 5155 // the segment queue holding unprocessed packets is limited to 500. 5156 const receiveBufferSize = 80 << 10 // 80KB. 5157 const maxReceiveBufferSize = receiveBufferSize * 10 5158 if err := stk.SetTransportProtocolOption(tcp.ProtocolNumber, tcp.ReceiveBufferSizeOption{1, receiveBufferSize, maxReceiveBufferSize}); err != nil { 5159 t.Fatalf("SetTransportProtocolOption failed: %v", err) 5160 } 5161 5162 // Enable auto-tuning. 5163 if err := stk.SetTransportProtocolOption(tcp.ProtocolNumber, tcpip.ModerateReceiveBufferOption(true)); err != nil { 5164 t.Fatalf("SetTransportProtocolOption failed: %v", err) 5165 } 5166 // Change the expected window scale to match the value needed for the 5167 // maximum buffer size used by stack. 5168 c.WindowScale = uint8(tcp.FindWndScale(maxReceiveBufferSize)) 5169 5170 rawEP := c.CreateConnectedWithOptions(header.TCPSynOptions{TS: true, WS: 4}) 5171 5172 wantRcvWnd := receiveBufferSize 5173 scaleRcvWnd := func(rcvWnd int) uint16 { 5174 return uint16(rcvWnd >> uint16(c.WindowScale)) 5175 } 5176 // Allocate a large array to send to the endpoint. 5177 b := make([]byte, receiveBufferSize*48) 5178 5179 // In every iteration we will send double the number of bytes sent in 5180 // the previous iteration and read the same from the app. The received 5181 // window should grow by at least 2x of bytes read by the app in every 5182 // RTT. 5183 offset := 0 5184 payloadSize := receiveBufferSize / 8 5185 worker := (c.EP).(interface { 5186 StopWork() 5187 ResumeWork() 5188 }) 5189 tsVal := rawEP.TSVal 5190 // We are going to do our own computation of what the moderated receive 5191 // buffer should be based on sent/copied data per RTT and verify that 5192 // the advertised window by the stack matches our calculations. 5193 prevCopied := 0 5194 done := false 5195 latency := 1 * time.Millisecond 5196 for i := 0; !done; i++ { 5197 tsVal++ 5198 5199 // Stop the worker goroutine. 5200 worker.StopWork() 5201 start := offset 5202 end := offset + payloadSize 5203 totalSent := 0 5204 packetsSent := 0 5205 for ; start < end; start += mss { 5206 rawEP.SendPacketWithTS(b[start:start+mss], tsVal) 5207 totalSent += mss 5208 packetsSent++ 5209 } 5210 // Resume it so that it only sees the packets once all of them 5211 // are waiting to be read. 5212 worker.ResumeWork() 5213 5214 // Give 1ms for the worker to process the packets. 5215 time.Sleep(1 * time.Millisecond) 5216 5217 // Verify that the advertised window on the ACK is reduced by 5218 // the total bytes sent. 5219 expectedWnd := wantRcvWnd - totalSent 5220 if packetsSent > 100 { 5221 for i := 0; i < (packetsSent / 100); i++ { 5222 _ = c.GetPacket() 5223 } 5224 } 5225 rawEP.VerifyACKRcvWnd(scaleRcvWnd(expectedWnd)) 5226 5227 // Now read all the data from the endpoint and invoke the 5228 // moderation API to allow for receive buffer auto-tuning 5229 // to happen before we measure the new window. 5230 totalCopied := 0 5231 for { 5232 b, _, err := c.EP.Read(nil) 5233 if err == tcpip.ErrWouldBlock { 5234 break 5235 } 5236 totalCopied += len(b) 5237 } 5238 5239 // Invoke the moderation API. This is required for auto-tuning 5240 // to happen. This method is normally expected to be invoked 5241 // from a higher layer than tcpip.Endpoint. So we simulate 5242 // copying to user-space by invoking it explicitly here. 5243 c.EP.ModerateRecvBuf(totalCopied) 5244 5245 // Now send a keep-alive packet to trigger an ACK so that we can 5246 // measure the new window. 5247 rawEP.NextSeqNum-- 5248 rawEP.SendPacketWithTS(nil, tsVal) 5249 rawEP.NextSeqNum++ 5250 5251 if i == 0 { 5252 // In the first iteration the receiver based RTT is not 5253 // yet known as a result the moderation code should not 5254 // increase the advertised window. 5255 rawEP.VerifyACKRcvWnd(scaleRcvWnd(wantRcvWnd)) 5256 prevCopied = totalCopied 5257 } else { 5258 rttCopied := totalCopied 5259 if i == 1 { 5260 // The moderation code accumulates copied bytes till 5261 // RTT is established. So add in the bytes sent in 5262 // the first iteration to the total bytes for this 5263 // RTT. 5264 rttCopied += prevCopied 5265 // Now reset it to the initial value used by the 5266 // auto tuning logic. 5267 prevCopied = tcp.InitialCwnd * mss * 2 5268 } 5269 newWnd := rttCopied<<1 + 16*mss 5270 grow := (newWnd * (rttCopied - prevCopied)) / prevCopied 5271 newWnd += (grow << 1) 5272 if newWnd > maxReceiveBufferSize { 5273 newWnd = maxReceiveBufferSize 5274 done = true 5275 } 5276 rawEP.VerifyACKRcvWnd(scaleRcvWnd(newWnd)) 5277 wantRcvWnd = newWnd 5278 prevCopied = rttCopied 5279 // Increase the latency after first two iterations to 5280 // establish a low RTT value in the receiver since it 5281 // only tracks the lowest value. This ensures that when 5282 // ModerateRcvBuf is called the elapsed time is always > 5283 // rtt. Without this the test is flaky due to delays due 5284 // to scheduling/wakeup etc. 5285 latency += 50 * time.Millisecond 5286 } 5287 time.Sleep(latency) 5288 offset += payloadSize 5289 payloadSize *= 2 5290 } 5291 } 5292 5293 func TestDelayEnabled(t *testing.T) { 5294 c := context.New(t, defaultMTU) 5295 defer c.Cleanup() 5296 checkDelayOption(t, c, false, 0) // Delay is disabled by default. 5297 5298 for _, v := range []struct { 5299 delayEnabled tcp.DelayEnabled 5300 wantDelayOption int 5301 }{ 5302 {delayEnabled: false, wantDelayOption: 0}, 5303 {delayEnabled: true, wantDelayOption: 1}, 5304 } { 5305 c := context.New(t, defaultMTU) 5306 defer c.Cleanup() 5307 if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, v.delayEnabled); err != nil { 5308 t.Fatalf("SetTransportProtocolOption(tcp, %t) failed: %v", v.delayEnabled, err) 5309 } 5310 checkDelayOption(t, c, v.delayEnabled, v.wantDelayOption) 5311 } 5312 } 5313 5314 func checkDelayOption(t *testing.T, c *context.Context, wantDelayEnabled tcp.DelayEnabled, wantDelayOption int) { 5315 t.Helper() 5316 5317 var gotDelayEnabled tcp.DelayEnabled 5318 if err := c.Stack().TransportProtocolOption(tcp.ProtocolNumber, &gotDelayEnabled); err != nil { 5319 t.Fatalf("TransportProtocolOption(tcp, &gotDelayEnabled) failed: %v", err) 5320 } 5321 if gotDelayEnabled != wantDelayEnabled { 5322 t.Errorf("TransportProtocolOption(tcp, &gotDelayEnabled) got %t, want %t", gotDelayEnabled, wantDelayEnabled) 5323 } 5324 5325 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, new(waiter.Queue)) 5326 if err != nil { 5327 t.Fatalf("NewEndPoint(tcp, ipv4, new(waiter.Queue)) failed: %v", err) 5328 } 5329 gotDelayOption, err := ep.GetSockOptInt(tcpip.DelayOption) 5330 if err != nil { 5331 t.Fatalf("ep.GetSockOptInt(tcpip.DelayOption) failed: %v", err) 5332 } 5333 if gotDelayOption != wantDelayOption { 5334 t.Errorf("ep.GetSockOptInt(tcpip.DelayOption) got: %d, want: %d", gotDelayOption, wantDelayOption) 5335 } 5336 } 5337 5338 func TestTCPLingerTimeout(t *testing.T) { 5339 c := context.New(t, 1500 /* mtu */) 5340 defer c.Cleanup() 5341 5342 c.CreateConnected(789, 30000, -1 /* epRcvBuf */) 5343 5344 testCases := []struct { 5345 name string 5346 tcpLingerTimeout time.Duration 5347 want time.Duration 5348 }{ 5349 {"NegativeLingerTimeout", -123123, 0}, 5350 {"ZeroLingerTimeout", 0, 0}, 5351 {"InRangeLingerTimeout", 10 * time.Second, 10 * time.Second}, 5352 // Values > stack's TCPLingerTimeout are capped to the stack's 5353 // value. Defaults to tcp.DefaultTCPLingerTimeout(60 seconds) 5354 {"AboveMaxLingerTimeout", 65 * time.Second, 60 * time.Second}, 5355 } 5356 for _, tc := range testCases { 5357 t.Run(tc.name, func(t *testing.T) { 5358 if err := c.EP.SetSockOpt(tcpip.TCPLingerTimeoutOption(tc.tcpLingerTimeout)); err != nil { 5359 t.Fatalf("SetSockOpt(%s) = %s", tc.tcpLingerTimeout, err) 5360 } 5361 var v tcpip.TCPLingerTimeoutOption 5362 if err := c.EP.GetSockOpt(&v); err != nil { 5363 t.Fatalf("GetSockOpt(tcpip.TCPLingerTimeoutOption) = %s", err) 5364 } 5365 if got, want := time.Duration(v), tc.want; got != want { 5366 t.Fatalf("unexpected linger timeout got: %s, want: %s", got, want) 5367 } 5368 }) 5369 } 5370 } 5371 5372 func TestTCPTimeWaitRSTIgnored(t *testing.T) { 5373 c := context.New(t, defaultMTU) 5374 defer c.Cleanup() 5375 5376 wq := &waiter.Queue{} 5377 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq) 5378 if err != nil { 5379 t.Fatalf("NewEndpoint failed: %s", err) 5380 } 5381 if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 5382 t.Fatalf("Bind failed: %s", err) 5383 } 5384 5385 if err := ep.Listen(10); err != nil { 5386 t.Fatalf("Listen failed: %s", err) 5387 } 5388 5389 // Send a SYN request. 5390 iss := seqnum.Value(789) 5391 c.SendPacket(nil, &context.Headers{ 5392 SrcPort: context.TestPort, 5393 DstPort: context.StackPort, 5394 Flags: header.TCPFlagSyn, 5395 SeqNum: iss, 5396 }) 5397 5398 // Receive the SYN-ACK reply. 5399 b := c.GetPacket() 5400 tcpHdr := header.TCP(header.IPv4(b).Payload()) 5401 c.IRS = seqnum.Value(tcpHdr.SequenceNumber()) 5402 5403 ackHeaders := &context.Headers{ 5404 SrcPort: context.TestPort, 5405 DstPort: context.StackPort, 5406 Flags: header.TCPFlagAck, 5407 SeqNum: iss + 1, 5408 AckNum: c.IRS + 1, 5409 } 5410 5411 // Send ACK. 5412 c.SendPacket(nil, ackHeaders) 5413 5414 // Try to accept the connection. 5415 we, ch := waiter.NewChannelEntry(nil) 5416 wq.EventRegister(&we, waiter.EventIn) 5417 defer wq.EventUnregister(&we) 5418 5419 c.EP, _, err = ep.Accept() 5420 if err == tcpip.ErrWouldBlock { 5421 // Wait for connection to be established. 5422 select { 5423 case <-ch: 5424 c.EP, _, err = ep.Accept() 5425 if err != nil { 5426 t.Fatalf("Accept failed: %s", err) 5427 } 5428 5429 case <-time.After(1 * time.Second): 5430 t.Fatalf("Timed out waiting for accept") 5431 } 5432 } 5433 5434 c.EP.Close() 5435 checker.IPv4(t, c.GetPacket(), checker.TCP( 5436 checker.SrcPort(context.StackPort), 5437 checker.DstPort(context.TestPort), 5438 checker.SeqNum(uint32(c.IRS+1)), 5439 checker.AckNum(uint32(iss)+1), 5440 checker.TCPFlags(header.TCPFlagFin|header.TCPFlagAck))) 5441 5442 finHeaders := &context.Headers{ 5443 SrcPort: context.TestPort, 5444 DstPort: context.StackPort, 5445 Flags: header.TCPFlagAck | header.TCPFlagFin, 5446 SeqNum: iss + 1, 5447 AckNum: c.IRS + 2, 5448 } 5449 5450 c.SendPacket(nil, finHeaders) 5451 5452 // Get the ACK to the FIN we just sent. 5453 checker.IPv4(t, c.GetPacket(), checker.TCP( 5454 checker.SrcPort(context.StackPort), 5455 checker.DstPort(context.TestPort), 5456 checker.SeqNum(uint32(c.IRS+2)), 5457 checker.AckNum(uint32(iss)+2), 5458 checker.TCPFlags(header.TCPFlagAck))) 5459 5460 // Now send a RST and this should be ignored and not 5461 // generate an ACK. 5462 c.SendPacket(nil, &context.Headers{ 5463 SrcPort: context.TestPort, 5464 DstPort: context.StackPort, 5465 Flags: header.TCPFlagRst, 5466 SeqNum: iss + 1, 5467 AckNum: c.IRS + 2, 5468 }) 5469 5470 c.CheckNoPacketTimeout("unexpected packet received in TIME_WAIT state", 1*time.Second) 5471 5472 // Out of order ACK should generate an immediate ACK in 5473 // TIME_WAIT. 5474 c.SendPacket(nil, &context.Headers{ 5475 SrcPort: context.TestPort, 5476 DstPort: context.StackPort, 5477 Flags: header.TCPFlagAck, 5478 SeqNum: iss + 1, 5479 AckNum: c.IRS + 3, 5480 }) 5481 5482 checker.IPv4(t, c.GetPacket(), checker.TCP( 5483 checker.SrcPort(context.StackPort), 5484 checker.DstPort(context.TestPort), 5485 checker.SeqNum(uint32(c.IRS+2)), 5486 checker.AckNum(uint32(iss)+2), 5487 checker.TCPFlags(header.TCPFlagAck))) 5488 } 5489 5490 func TestTCPTimeWaitOutOfOrder(t *testing.T) { 5491 c := context.New(t, defaultMTU) 5492 defer c.Cleanup() 5493 5494 wq := &waiter.Queue{} 5495 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq) 5496 if err != nil { 5497 t.Fatalf("NewEndpoint failed: %s", err) 5498 } 5499 if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 5500 t.Fatalf("Bind failed: %s", err) 5501 } 5502 5503 if err := ep.Listen(10); err != nil { 5504 t.Fatalf("Listen failed: %s", err) 5505 } 5506 5507 // Send a SYN request. 5508 iss := seqnum.Value(789) 5509 c.SendPacket(nil, &context.Headers{ 5510 SrcPort: context.TestPort, 5511 DstPort: context.StackPort, 5512 Flags: header.TCPFlagSyn, 5513 SeqNum: iss, 5514 }) 5515 5516 // Receive the SYN-ACK reply. 5517 b := c.GetPacket() 5518 tcpHdr := header.TCP(header.IPv4(b).Payload()) 5519 c.IRS = seqnum.Value(tcpHdr.SequenceNumber()) 5520 5521 ackHeaders := &context.Headers{ 5522 SrcPort: context.TestPort, 5523 DstPort: context.StackPort, 5524 Flags: header.TCPFlagAck, 5525 SeqNum: iss + 1, 5526 AckNum: c.IRS + 1, 5527 } 5528 5529 // Send ACK. 5530 c.SendPacket(nil, ackHeaders) 5531 5532 // Try to accept the connection. 5533 we, ch := waiter.NewChannelEntry(nil) 5534 wq.EventRegister(&we, waiter.EventIn) 5535 defer wq.EventUnregister(&we) 5536 5537 c.EP, _, err = ep.Accept() 5538 if err == tcpip.ErrWouldBlock { 5539 // Wait for connection to be established. 5540 select { 5541 case <-ch: 5542 c.EP, _, err = ep.Accept() 5543 if err != nil { 5544 t.Fatalf("Accept failed: %s", err) 5545 } 5546 5547 case <-time.After(1 * time.Second): 5548 t.Fatalf("Timed out waiting for accept") 5549 } 5550 } 5551 5552 c.EP.Close() 5553 checker.IPv4(t, c.GetPacket(), checker.TCP( 5554 checker.SrcPort(context.StackPort), 5555 checker.DstPort(context.TestPort), 5556 checker.SeqNum(uint32(c.IRS+1)), 5557 checker.AckNum(uint32(iss)+1), 5558 checker.TCPFlags(header.TCPFlagFin|header.TCPFlagAck))) 5559 5560 finHeaders := &context.Headers{ 5561 SrcPort: context.TestPort, 5562 DstPort: context.StackPort, 5563 Flags: header.TCPFlagAck | header.TCPFlagFin, 5564 SeqNum: iss + 1, 5565 AckNum: c.IRS + 2, 5566 } 5567 5568 c.SendPacket(nil, finHeaders) 5569 5570 // Get the ACK to the FIN we just sent. 5571 checker.IPv4(t, c.GetPacket(), checker.TCP( 5572 checker.SrcPort(context.StackPort), 5573 checker.DstPort(context.TestPort), 5574 checker.SeqNum(uint32(c.IRS+2)), 5575 checker.AckNum(uint32(iss)+2), 5576 checker.TCPFlags(header.TCPFlagAck))) 5577 5578 // Out of order ACK should generate an immediate ACK in 5579 // TIME_WAIT. 5580 c.SendPacket(nil, &context.Headers{ 5581 SrcPort: context.TestPort, 5582 DstPort: context.StackPort, 5583 Flags: header.TCPFlagAck, 5584 SeqNum: iss + 1, 5585 AckNum: c.IRS + 3, 5586 }) 5587 5588 checker.IPv4(t, c.GetPacket(), checker.TCP( 5589 checker.SrcPort(context.StackPort), 5590 checker.DstPort(context.TestPort), 5591 checker.SeqNum(uint32(c.IRS+2)), 5592 checker.AckNum(uint32(iss)+2), 5593 checker.TCPFlags(header.TCPFlagAck))) 5594 } 5595 5596 func TestTCPTimeWaitNewSyn(t *testing.T) { 5597 c := context.New(t, defaultMTU) 5598 defer c.Cleanup() 5599 5600 wq := &waiter.Queue{} 5601 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq) 5602 if err != nil { 5603 t.Fatalf("NewEndpoint failed: %s", err) 5604 } 5605 if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 5606 t.Fatalf("Bind failed: %s", err) 5607 } 5608 5609 if err := ep.Listen(10); err != nil { 5610 t.Fatalf("Listen failed: %s", err) 5611 } 5612 5613 // Send a SYN request. 5614 iss := seqnum.Value(789) 5615 c.SendPacket(nil, &context.Headers{ 5616 SrcPort: context.TestPort, 5617 DstPort: context.StackPort, 5618 Flags: header.TCPFlagSyn, 5619 SeqNum: iss, 5620 }) 5621 5622 // Receive the SYN-ACK reply. 5623 b := c.GetPacket() 5624 tcpHdr := header.TCP(header.IPv4(b).Payload()) 5625 c.IRS = seqnum.Value(tcpHdr.SequenceNumber()) 5626 5627 ackHeaders := &context.Headers{ 5628 SrcPort: context.TestPort, 5629 DstPort: context.StackPort, 5630 Flags: header.TCPFlagAck, 5631 SeqNum: iss + 1, 5632 AckNum: c.IRS + 1, 5633 } 5634 5635 // Send ACK. 5636 c.SendPacket(nil, ackHeaders) 5637 5638 // Try to accept the connection. 5639 we, ch := waiter.NewChannelEntry(nil) 5640 wq.EventRegister(&we, waiter.EventIn) 5641 defer wq.EventUnregister(&we) 5642 5643 c.EP, _, err = ep.Accept() 5644 if err == tcpip.ErrWouldBlock { 5645 // Wait for connection to be established. 5646 select { 5647 case <-ch: 5648 c.EP, _, err = ep.Accept() 5649 if err != nil { 5650 t.Fatalf("Accept failed: %s", err) 5651 } 5652 5653 case <-time.After(1 * time.Second): 5654 t.Fatalf("Timed out waiting for accept") 5655 } 5656 } 5657 5658 c.EP.Close() 5659 checker.IPv4(t, c.GetPacket(), checker.TCP( 5660 checker.SrcPort(context.StackPort), 5661 checker.DstPort(context.TestPort), 5662 checker.SeqNum(uint32(c.IRS+1)), 5663 checker.AckNum(uint32(iss)+1), 5664 checker.TCPFlags(header.TCPFlagFin|header.TCPFlagAck))) 5665 5666 finHeaders := &context.Headers{ 5667 SrcPort: context.TestPort, 5668 DstPort: context.StackPort, 5669 Flags: header.TCPFlagAck | header.TCPFlagFin, 5670 SeqNum: iss + 1, 5671 AckNum: c.IRS + 2, 5672 } 5673 5674 c.SendPacket(nil, finHeaders) 5675 5676 // Get the ACK to the FIN we just sent. 5677 checker.IPv4(t, c.GetPacket(), checker.TCP( 5678 checker.SrcPort(context.StackPort), 5679 checker.DstPort(context.TestPort), 5680 checker.SeqNum(uint32(c.IRS+2)), 5681 checker.AckNum(uint32(iss)+2), 5682 checker.TCPFlags(header.TCPFlagAck))) 5683 5684 // Send a SYN request w/ sequence number lower than 5685 // the highest sequence number sent. We just reuse 5686 // the same number. 5687 iss = seqnum.Value(789) 5688 c.SendPacket(nil, &context.Headers{ 5689 SrcPort: context.TestPort, 5690 DstPort: context.StackPort, 5691 Flags: header.TCPFlagSyn, 5692 SeqNum: iss, 5693 }) 5694 5695 c.CheckNoPacketTimeout("unexpected packet received in response to SYN", 1*time.Second) 5696 5697 // Send a SYN request w/ sequence number higher than 5698 // the highest sequence number sent. 5699 iss = seqnum.Value(792) 5700 c.SendPacket(nil, &context.Headers{ 5701 SrcPort: context.TestPort, 5702 DstPort: context.StackPort, 5703 Flags: header.TCPFlagSyn, 5704 SeqNum: iss, 5705 }) 5706 5707 // Receive the SYN-ACK reply. 5708 b = c.GetPacket() 5709 tcpHdr = header.TCP(header.IPv4(b).Payload()) 5710 c.IRS = seqnum.Value(tcpHdr.SequenceNumber()) 5711 5712 ackHeaders = &context.Headers{ 5713 SrcPort: context.TestPort, 5714 DstPort: context.StackPort, 5715 Flags: header.TCPFlagAck, 5716 SeqNum: iss + 1, 5717 AckNum: c.IRS + 1, 5718 } 5719 5720 // Send ACK. 5721 c.SendPacket(nil, ackHeaders) 5722 5723 // Try to accept the connection. 5724 c.EP, _, err = ep.Accept() 5725 if err == tcpip.ErrWouldBlock { 5726 // Wait for connection to be established. 5727 select { 5728 case <-ch: 5729 c.EP, _, err = ep.Accept() 5730 if err != nil { 5731 t.Fatalf("Accept failed: %s", err) 5732 } 5733 5734 case <-time.After(1 * time.Second): 5735 t.Fatalf("Timed out waiting for accept") 5736 } 5737 } 5738 } 5739 5740 func TestTCPTimeWaitDuplicateFINExtendsTimeWait(t *testing.T) { 5741 c := context.New(t, defaultMTU) 5742 defer c.Cleanup() 5743 5744 // Set TCPTimeWaitTimeout to 5 seconds so that sockets are marked closed 5745 // after 5 seconds in TIME_WAIT state. 5746 tcpTimeWaitTimeout := 5 * time.Second 5747 if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, tcpip.TCPTimeWaitTimeoutOption(tcpTimeWaitTimeout)); err != nil { 5748 t.Fatalf("c.stack.SetTransportProtocolOption(tcp, tcpip.TCPLingerTimeoutOption(%d) failed: %s", tcpTimeWaitTimeout, err) 5749 } 5750 5751 wq := &waiter.Queue{} 5752 ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq) 5753 if err != nil { 5754 t.Fatalf("NewEndpoint failed: %s", err) 5755 } 5756 if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil { 5757 t.Fatalf("Bind failed: %s", err) 5758 } 5759 5760 if err := ep.Listen(10); err != nil { 5761 t.Fatalf("Listen failed: %s", err) 5762 } 5763 5764 // Send a SYN request. 5765 iss := seqnum.Value(789) 5766 c.SendPacket(nil, &context.Headers{ 5767 SrcPort: context.TestPort, 5768 DstPort: context.StackPort, 5769 Flags: header.TCPFlagSyn, 5770 SeqNum: iss, 5771 }) 5772 5773 // Receive the SYN-ACK reply. 5774 b := c.GetPacket() 5775 tcpHdr := header.TCP(header.IPv4(b).Payload()) 5776 c.IRS = seqnum.Value(tcpHdr.SequenceNumber()) 5777 5778 ackHeaders := &context.Headers{ 5779 SrcPort: context.TestPort, 5780 DstPort: context.StackPort, 5781 Flags: header.TCPFlagAck, 5782 SeqNum: iss + 1, 5783 AckNum: c.IRS + 1, 5784 } 5785 5786 // Send ACK. 5787 c.SendPacket(nil, ackHeaders) 5788 5789 // Try to accept the connection. 5790 we, ch := waiter.NewChannelEntry(nil) 5791 wq.EventRegister(&we, waiter.EventIn) 5792 defer wq.EventUnregister(&we) 5793 5794 c.EP, _, err = ep.Accept() 5795 if err == tcpip.ErrWouldBlock { 5796 // Wait for connection to be established. 5797 select { 5798 case <-ch: 5799 c.EP, _, err = ep.Accept() 5800 if err != nil { 5801 t.Fatalf("Accept failed: %s", err) 5802 } 5803 5804 case <-time.After(1 * time.Second): 5805 t.Fatalf("Timed out waiting for accept") 5806 } 5807 } 5808 5809 c.EP.Close() 5810 checker.IPv4(t, c.GetPacket(), checker.TCP( 5811 checker.SrcPort(context.StackPort), 5812 checker.DstPort(context.TestPort), 5813 checker.SeqNum(uint32(c.IRS+1)), 5814 checker.AckNum(uint32(iss)+1), 5815 checker.TCPFlags(header.TCPFlagFin|header.TCPFlagAck))) 5816 5817 finHeaders := &context.Headers{ 5818 SrcPort: context.TestPort, 5819 DstPort: context.StackPort, 5820 Flags: header.TCPFlagAck | header.TCPFlagFin, 5821 SeqNum: iss + 1, 5822 AckNum: c.IRS + 2, 5823 } 5824 5825 c.SendPacket(nil, finHeaders) 5826 5827 // Get the ACK to the FIN we just sent. 5828 checker.IPv4(t, c.GetPacket(), checker.TCP( 5829 checker.SrcPort(context.StackPort), 5830 checker.DstPort(context.TestPort), 5831 checker.SeqNum(uint32(c.IRS+2)), 5832 checker.AckNum(uint32(iss)+2), 5833 checker.TCPFlags(header.TCPFlagAck))) 5834 5835 time.Sleep(2 * time.Second) 5836 5837 // Now send a duplicate FIN. This should cause the TIME_WAIT to extend 5838 // by another 5 seconds and also send us a duplicate ACK as it should 5839 // indicate that the final ACK was potentially lost. 5840 c.SendPacket(nil, finHeaders) 5841 5842 // Get the ACK to the FIN we just sent. 5843 checker.IPv4(t, c.GetPacket(), checker.TCP( 5844 checker.SrcPort(context.StackPort), 5845 checker.DstPort(context.TestPort), 5846 checker.SeqNum(uint32(c.IRS+2)), 5847 checker.AckNum(uint32(iss)+2), 5848 checker.TCPFlags(header.TCPFlagAck))) 5849 5850 // Sleep for 4 seconds so at this point we are 1 second past the 5851 // original tcpLingerTimeout of 5 seconds. 5852 time.Sleep(4 * time.Second) 5853 5854 // Send an ACK and it should not generate any packet as the socket 5855 // should still be in TIME_WAIT for another another 5 seconds due 5856 // to the duplicate FIN we sent earlier. 5857 *ackHeaders = *finHeaders 5858 ackHeaders.SeqNum = ackHeaders.SeqNum + 1 5859 ackHeaders.Flags = header.TCPFlagAck 5860 c.SendPacket(nil, ackHeaders) 5861 5862 c.CheckNoPacketTimeout("unexpected packet received from endpoint in TIME_WAIT", 1*time.Second) 5863 // Now sleep for another 2 seconds so that we are past the 5864 // extended TIME_WAIT of 7 seconds (2 + 5). 5865 time.Sleep(2 * time.Second) 5866 5867 // Resend the same ACK. 5868 c.SendPacket(nil, ackHeaders) 5869 5870 // Receive the RST that should be generated as there is no valid 5871 // endpoint. 5872 checker.IPv4(t, c.GetPacket(), checker.TCP( 5873 checker.SrcPort(context.StackPort), 5874 checker.DstPort(context.TestPort), 5875 checker.SeqNum(uint32(ackHeaders.AckNum)), 5876 checker.AckNum(uint32(ackHeaders.SeqNum)), 5877 checker.TCPFlags(header.TCPFlagRst|header.TCPFlagAck))) 5878 }