| // Copyright (c) 2019 The Chromium Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "quiche/quic/qbone/platform/tcp_packet.h" | |
| #include <netinet/ip6.h> | |
| #include <netinet/tcp.h> | |
| #include <cstdint> | |
| #include <cstring> | |
| #include <string> | |
| #include "absl/strings/string_view.h" | |
| #include "quiche/quic/platform/api/quic_test.h" | |
| #include "quiche/common/quiche_text_utils.h" | |
| namespace quic { | |
| namespace { | |
| // clang-format off | |
| constexpr uint8_t kReferenceTCPSYNPacket[] = { | |
| // START IPv6 Header | |
| // IPv6 with zero ToS and flow label | |
| 0x60, 0x00, 0x00, 0x00, | |
| // Payload is 40 bytes | |
| 0x00, 0x28, | |
| // Next header is TCP (6) | |
| 0x06, | |
| // Hop limit is 64 | |
| 0x40, | |
| // Source address of ::1 | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, | |
| // Destination address of ::1 | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, | |
| // END IPv6 Header | |
| // START TCPv6 Header | |
| // Source port | |
| 0xac, 0x1e, | |
| // Destination port | |
| 0x27, 0x0f, | |
| // Sequence number | |
| 0x4b, 0x01, 0xe8, 0x99, | |
| // Acknowledgement Sequence number, | |
| 0x00, 0x00, 0x00, 0x00, | |
| // Offset | |
| 0xa0, | |
| // Flags | |
| 0x02, | |
| // Window | |
| 0xaa, 0xaa, | |
| // Checksum | |
| 0x2e, 0x21, | |
| // Urgent | |
| 0x00, 0x00, | |
| // END TCPv6 Header | |
| // Options | |
| 0x02, 0x04, 0xff, 0xc4, 0x04, 0x02, 0x08, 0x0a, | |
| 0x1b, 0xb8, 0x52, 0xa1, 0x00, 0x00, 0x00, 0x00, | |
| 0x01, 0x03, 0x03, 0x07, | |
| }; | |
| constexpr uint8_t kReferenceTCPRSTPacket[] = { | |
| // START IPv6 Header | |
| // IPv6 with zero ToS and flow label | |
| 0x60, 0x00, 0x00, 0x00, | |
| // Payload is 20 bytes | |
| 0x00, 0x14, | |
| // Next header is TCP (6) | |
| 0x06, | |
| // Hop limit is 64 | |
| 0x40, | |
| // Source address of ::1 | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, | |
| // Destination address of ::1 | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, | |
| // END IPv6 Header | |
| // START TCPv6 Header | |
| // Source port | |
| 0x27, 0x0f, | |
| // Destination port | |
| 0xac, 0x1e, | |
| // Sequence number | |
| 0x00, 0x00, 0x00, 0x00, | |
| // Acknowledgement Sequence number, | |
| 0x4b, 0x01, 0xe8, 0x9a, | |
| // Offset | |
| 0x50, | |
| // Flags | |
| 0x14, | |
| // Window | |
| 0x00, 0x00, | |
| // Checksum | |
| 0xa9, 0x05, | |
| // Urgent | |
| 0x00, 0x00, | |
| // END TCPv6 Header | |
| }; | |
| // clang-format on | |
| } // namespace | |
| TEST(TcpPacketTest, CreatedPacketMatchesReference) { | |
| absl::string_view syn = | |
| absl::string_view(reinterpret_cast<const char*>(kReferenceTCPSYNPacket), | |
| sizeof(kReferenceTCPSYNPacket)); | |
| absl::string_view expected_packet = | |
| absl::string_view(reinterpret_cast<const char*>(kReferenceTCPRSTPacket), | |
| sizeof(kReferenceTCPRSTPacket)); | |
| bool callback_called = false; | |
| CreateTcpResetPacket(syn, [&](absl::string_view packet) { | |
| callback_called = true; | |
| QUIC_LOG(INFO) << quiche::QuicheTextUtils::HexDump(packet); | |
| ASSERT_EQ(packet, expected_packet); | |
| }); | |
| EXPECT_TRUE(callback_called); | |
| } | |
| TEST(TcpPacketTest, PacketTooShortForTcpHeaderDoesNotGenerateReset) { | |
| absl::string_view truncated_packet( | |
| reinterpret_cast<const char*>(kReferenceTCPSYNPacket), | |
| sizeof(ip6_hdr) + sizeof(tcphdr) - 1); | |
| bool callback_called = false; | |
| CreateTcpResetPacket(truncated_packet, | |
| [&](absl::string_view) { callback_called = true; }); | |
| EXPECT_FALSE(callback_called); | |
| } | |
| TEST(TcpPacketTest, ResetForPacketWithAckTakesSequenceFromAckSeq) { | |
| std::string packet(reinterpret_cast<const char*>(kReferenceTCPSYNPacket), | |
| sizeof(kReferenceTCPSYNPacket)); | |
| ip6_hdr ip6; | |
| memcpy(&ip6, packet.data(), sizeof(ip6_hdr)); | |
| ip6.ip6_dst.s6_addr[15] = 2; | |
| memcpy(packet.data(), &ip6, sizeof(ip6_hdr)); | |
| tcphdr tcp_hdr; | |
| memcpy(&tcp_hdr, packet.data() + sizeof(ip6_hdr), sizeof(tcphdr)); | |
| tcp_hdr.ack = 1; | |
| tcp_hdr.ack_seq = 0x12345678; | |
| memcpy(packet.data() + sizeof(ip6_hdr), &tcp_hdr, sizeof(tcphdr)); | |
| bool callback_called = false; | |
| CreateTcpResetPacket(packet, [&](absl::string_view rst_packet) { | |
| callback_called = true; | |
| ASSERT_EQ(rst_packet.size(), sizeof(ip6_hdr) + sizeof(tcphdr)); | |
| ip6_hdr rst_ip6; | |
| memcpy(&rst_ip6, rst_packet.data(), sizeof(ip6_hdr)); | |
| EXPECT_EQ(rst_ip6.ip6_src.s6_addr[15], 2); | |
| EXPECT_EQ(rst_ip6.ip6_dst.s6_addr[15], 1); | |
| tcphdr rst_tcp; | |
| memcpy(&rst_tcp, rst_packet.data() + sizeof(ip6_hdr), sizeof(tcphdr)); | |
| EXPECT_EQ(rst_tcp.rst, 1); | |
| EXPECT_EQ(rst_tcp.ack, 0); | |
| EXPECT_EQ(rst_tcp.seq, 0x12345678); | |
| EXPECT_EQ(rst_tcp.source, tcp_hdr.dest); | |
| EXPECT_EQ(rst_tcp.dest, tcp_hdr.source); | |
| }); | |
| EXPECT_TRUE(callback_called); | |
| } | |
| } // namespace quic |