test: add imitate_udp_packet test case (UDP over GER, with GRE payload length, with UDP payload)

This commit is contained in:
luwenpeng
2024-07-30 18:21:51 +08:00
parent 1f09f43f52
commit 18153550d9

View File

@@ -783,6 +783,237 @@ TEST(PACKET_CRAFT_TCP, ETH_IP4_GRE_IP6_TCP)
} }
#endif #endif
/******************************************************************************
* [Protocols in frame: eth:ethertype:vlan:ethertype:ipv6:ip:gre:ppp:ip:udp:dns]
******************************************************************************
*
* Frame 1: 197 bytes on wire (1576 bits), 197 bytes captured (1576 bits)
* Ethernet II, Src: JuniperNetwo_f2:61:3d (00:12:1e:f2:61:3d), Dst: c5:00:00:00:82:c4 (c5:00:00:00:82:c4)
* Destination: c5:00:00:00:82:c4 (c5:00:00:00:82:c4)
* Source: JuniperNetwo_f2:61:3d (00:12:1e:f2:61:3d)
* Type: 802.1Q Virtual LAN (0x8100)
* 802.1Q Virtual LAN, PRI: 0, DEI: 0, ID: 100
* 000. .... .... .... = Priority: Best Effort (default) (0)
* ...0 .... .... .... = DEI: Ineligible
* .... 0000 0110 0100 = ID: 100
* Type: IPv6 (0x86dd)
* Internet Protocol Version 6, Src: 2402:f000:1:8e01::5555, Dst: 2607:fcd0:100:2300::b108:2a6b
* 0110 .... = Version: 6
* .... 0000 0000 .... .... .... .... .... = Traffic Class: 0x00 (DSCP: CS0, ECN: Not-ECT)
* .... 0000 00.. .... .... .... .... .... = Differentiated Services Codepoint: Default (0)
* .... .... ..00 .... .... .... .... .... = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* .... 0000 0000 0000 0000 0000 = Flow Label: 0x00000
* Payload Length: 139
* Next Header: IPIP (4)
* Hop Limit: 246
* Source Address: 2402:f000:1:8e01::5555
* Destination Address: 2607:fcd0:100:2300::b108:2a6b
* Internet Protocol Version 4, Src: 16.0.0.200, Dst: 192.52.166.154
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* 0000 00.. = Differentiated Services Codepoint: Default (0)
* .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* Total Length: 139
* Identification: 0x8caf (36015)
* 000. .... = Flags: 0x0
* 0... .... = Reserved bit: Not set
* .0.. .... = Don't fragment: Not set
* ..0. .... = More fragments: Not set
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 64
* Protocol: Generic Routing Encapsulation (47)
* Header Checksum: 0x75fe [correct]
* [Header checksum status: Good]
* [Calculated Checksum: 0x75fe]
* Source Address: 16.0.0.200
* Destination Address: 192.52.166.154
* Generic Routing Encapsulation (PPP)
* Flags and Version: 0x3081
* 0... .... .... .... = Checksum Bit: No
* .0.. .... .... .... = Routing Bit: No
* ..1. .... .... .... = Key Bit: Yes
* ...1 .... .... .... = Sequence Number Bit: Yes
* .... 0... .... .... = Strict Source Route Bit: No
* .... .000 .... .... = Recursion control: 0
* .... .... 1... .... = Acknowledgment: Yes
* .... .... .000 0... = Flags (Reserved): 0
* .... .... .... .001 = Version: Enhanced GRE (1)
* Protocol Type: PPP (0x880b)
* Payload Length: 103
* Call ID: 6016
* Sequence Number: 430001
* Acknowledgment Number: 539254
* Point-to-Point Protocol
* Address: 0xff
* Control: 0x03
* Protocol: Internet Protocol version 4 (0x0021)
* Internet Protocol Version 4, Src: 172.16.44.3, Dst: 8.8.8.8
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* 0000 00.. = Differentiated Services Codepoint: Default (0)
* .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* Total Length: 99
* Identification: 0x0000 (0)
* 010. .... = Flags: 0x2, Don't fragment
* 0... .... = Reserved bit: Not set
* .1.. .... = Don't fragment: Set
* ..0. .... = More fragments: Not set
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 60
* Protocol: UDP (17)
* Header Checksum: 0x5667 [correct]
* [Header checksum status: Good]
* [Calculated Checksum: 0x5667]
* Source Address: 172.16.44.3
* Destination Address: 8.8.8.8
* User Datagram Protocol, Src Port: 40768, Dst Port: 53
* Source Port: 40768
* Destination Port: 53
* Length: 79
* Checksum: 0x2d23 [correct]
* [Calculated Checksum: 0x2d23]
* [Checksum Status: Good]
* [Stream index: 0]
* [Timestamps]
* [Time since first frame: 0.000000000 seconds]
* [Time since previous frame: 0.000000000 seconds]
* UDP payload (71 bytes)
* Domain Name System (query)
* Transaction ID: 0xa62c
* Flags: 0x0100 Standard query
* 0... .... .... .... = Response: Message is a query
* .000 0... .... .... = Opcode: Standard query (0)
* .... ..0. .... .... = Truncated: Message is not truncated
* .... ...1 .... .... = Recursion desired: Do query recursively
* .... .... .0.. .... = Z: reserved (0)
* .... .... ...0 .... = Non-authenticated data: Unacceptable
* Questions: 1
* Answer RRs: 0
* Authority RRs: 0
* Additional RRs: 0
* Queries
* xqt-detect-mode2-97712e88-167a-45b9-93ee-913140e76678: type AAAA, class IN
* Name: xqt-detect-mode2-97712e88-167a-45b9-93ee-913140e76678
* [Name Length: 53]
* [Label Count: 1]
* Type: AAAA (28) (IP6 Address)
* Class: IN (0x0001)
* [Response In: 2]
*/
unsigned char data5[] = {
0xc5, 0x00, 0x00, 0x00, 0x82, 0xc4, 0x00, 0x12, 0x1e, 0xf2, 0x61, 0x3d, 0x81, 0x00, 0x00, 0x64, 0x86, 0xdd, 0x60, 0x00, 0x00, 0x00, 0x00, 0x8b, 0x04, 0xf6,
0x24, 0x02, 0xf0, 0x00, 0x00, 0x01, 0x8e, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x55, 0x26, 0x07, 0xfc, 0xd0, 0x01, 0x00, 0x23, 0x00, 0x00, 0x00,
0x00, 0x00, 0xb1, 0x08, 0x2a, 0x6b, 0x45, 0x00, 0x00, 0x8b, 0x8c, 0xaf, 0x00, 0x00, 0x40, 0x2f, 0x75, 0xfe, 0x10, 0x00, 0x00, 0xc8, 0xc0, 0x34, 0xa6, 0x9a,
0x30, 0x81, 0x88, 0x0b, 0x00, 0x67, 0x17, 0x80, 0x00, 0x06, 0x8f, 0xb1, 0x00, 0x08, 0x3a, 0x76, 0xff, 0x03, 0x00, 0x21, 0x45, 0x00, 0x00, 0x63, 0x00, 0x00,
0x40, 0x00, 0x3c, 0x11, 0x56, 0x67, 0xac, 0x10, 0x2c, 0x03, 0x08, 0x08, 0x08, 0x08, 0x9f, 0x40, 0x00, 0x35, 0x00, 0x4f, 0x2d, 0x23, 0xa6, 0x2c, 0x01, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x35, 0x78, 0x71, 0x74, 0x2d, 0x64, 0x65, 0x74, 0x65, 0x63, 0x74, 0x2d, 0x6d, 0x6f, 0x64, 0x65, 0x32, 0x2d,
0x39, 0x37, 0x37, 0x31, 0x32, 0x65, 0x38, 0x38, 0x2d, 0x31, 0x36, 0x37, 0x61, 0x2d, 0x34, 0x35, 0x62, 0x39, 0x2d, 0x39, 0x33, 0x65, 0x65, 0x2d, 0x39, 0x31,
0x33, 0x31, 0x34, 0x30, 0x65, 0x37, 0x36, 0x36, 0x37, 0x38, 0x00, 0x00, 0x1c, 0x00, 0x01};
/*
* craft_udp_packet()
* -> ETH->VLAN->IPv6->IPv4->GRE->PPP->IPv4->UDP->DNS
* -> with UDP payload
* -> with GRE payload length
*/
#if 1
TEST(PACKET_CRAFT_UDP, ETH_VLAN_IPv6_IPv4_GRE_PPP_IPv4_UDP_DNS)
{
struct packet orig_pkt;
memset(&orig_pkt, 0, sizeof(orig_pkt));
packet_parse(&orig_pkt, (const char *)data5, sizeof(data5));
PRINT_GREEN("origin packet:");
packet_print(&orig_pkt);
struct packet *new_pkt = craft_udp_packet(&orig_pkt, "Hello", 5);
EXPECT_TRUE(new_pkt != nullptr);
PRINT_GREEN("new packet:");
packet_print(new_pkt);
packet_dump_hex(new_pkt, STDOUT_FILENO);
packet_dump_pcap(new_pkt, "craft-eth-vlan-ipv6-ipv4-gre-ppp-ipv4-udp-dns.pcap");
const char *orig_pkt_data = packet_get_raw_data(&orig_pkt);
uint16_t orig_pkt_len = packet_get_raw_len(&orig_pkt);
const char *new_pkt_data = packet_get_raw_data(new_pkt);
uint16_t new_pkt_len = packet_get_raw_len(new_pkt);
EXPECT_TRUE(orig_pkt_len - 71 == // trim DNS payload
new_pkt_len - 5); // trim UDP payload
int count = 0;
struct layer layer;
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
{
if (layer.proto == LAYER_PROTO_IPV6)
{
const struct ip6_hdr *ip6 = (const struct ip6_hdr *)layer.hdr.raw;
EXPECT_TRUE(ip6_hdr_get_payload_len(ip6) == 73);
}
if (layer.proto == LAYER_PROTO_IPV4)
{
const struct ip *ip = (const struct ip *)layer.hdr.raw;
if (count == 0)
{
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 73);
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x7640);
}
else
{
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 33);
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x56a9);
}
count++;
}
if (layer.proto == LAYER_PROTO_GRE)
{
const struct gre1_hdr *gre = (const struct gre1_hdr *)layer.hdr.raw;
EXPECT_TRUE(gre1_hdr_get_version(gre) == 1);
EXPECT_TRUE(gre1_hdr_get_payload_length(gre) == 37);
}
if (layer.proto == LAYER_PROTO_UDP)
{
const struct udphdr *udp = (const struct udphdr *)layer.hdr.raw;
EXPECT_TRUE(udp_hdr_get_total_len(udp) == 13);
EXPECT_TRUE(udp_hdr_get_checksum(udp) == 0x5469);
}
}
for (uint16_t i = 0; i < new_pkt_len - 5; i++)
{
if (22 <= i && i <= 23) /// skip IPv6 payload length
{
continue;
}
if ((60 <= i && i <= 61) || // skip IPv4 total length
(68 <= i && i <= 69)) // skip IPv4 checksum
{
continue;
}
if ((82 <= i && i <= 83)) // skip GRE payload length
{
continue;
}
if ((100 <= i && i <= 101) || // skip IPv4 total length
(108 <= i && i <= 109)) // skip IPv4 checksum
{
continue;
}
if ((122 <= i && i <= 123) || // skip UDP total length
(124 <= i && i <= 125)) // skip UDP checksum
{
continue;
}
printf(("idx: %d, orig: %02x, new: %02x\n"), i, orig_pkt_data[i], new_pkt_data[i]);
EXPECT_TRUE(orig_pkt_data[i] == new_pkt_data[i]);
}
}
#endif
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
::testing::InitGoogleTest(&argc, argv); ::testing::InitGoogleTest(&argc, argv);