From 6c19f98393aa20d6ab56b0afa563084e573c4728 Mon Sep 17 00:00:00 2001 From: Vasil Velichkov Date: Wed, 10 Oct 2018 00:40:04 +0300 Subject: [PATCH] text2pcap: Fix TCP, UDP or SCTP headers over IPv6 When the IPv6 (-6) option was specified together with either TCP (-T), UDP (-u) or SCTP (-s/-S) option the generated packet was invalid because an IPv4 option was implied an a wrong header was added. Bug: 15194 Change-Id: I5a7b83d8aa3f3ad56f0c8110e598090945e60225 Reviewed-on: https://code.wireshark.org/review/30107 Petri-Dish: Peter Wu Tested-by: Petri Dish Buildbot Reviewed-by: Peter Wu --- test/suite_text2pcap.py | 129 ++++++++++++++++++++++++++++++++++++++++ text2pcap.c | 14 +++-- 2 files changed, 139 insertions(+), 4 deletions(-) diff --git a/test/suite_text2pcap.py b/test/suite_text2pcap.py index 1a1534515f..2a0e75f1dd 100644 --- a/test/suite_text2pcap.py +++ b/test/suite_text2pcap.py @@ -296,4 +296,133 @@ class case_text2pcap_eol_hash(subprocesstest.SubprocessTestCase): # test_step_ok # } +def run_text2pcap_content(test, content, args): + testin_file = test.filename_from_id(testin_txt) + testout_file = test.filename_from_id(testout_pcap) + fin = open(testin_file, "w") + fin.write(content) + fin.close() + + test.assertRun((config.cmd_text2pcap,) + args + (testin_file, testout_file)) + return testout_file + +class case_text2pcap_headers(subprocesstest.SubprocessTestCase): + '''Test TCP, UDP or SCTP header without -4 or -6 option''' + + def run_text2pcap(self, content, args): + return run_text2pcap_content(self, content, args) + + def test_text2pcap_tcp(self): + '''Test TCP over IPv4''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 60}, + get_capinfos_cmp_info(check_capinfos_info(self, + self.run_text2pcap("0000: ff ff ff ff\n", + ("-T", "1234,1234"))))) + + def test_text2pcap_udp(self): + '''Test UDP over IPv4''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 60}, + get_capinfos_cmp_info(check_capinfos_info(self, + self.run_text2pcap("0000: ff ff ff ff\n", + ("-u", "1234,1234"))))) + + def test_text2pcap_sctp(self): + '''Test SCTP over IPv4''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 70}, + get_capinfos_cmp_info(check_capinfos_info(self, + self.run_text2pcap( + "0000 00 03 00 18 00 00 00 00 00 00 00 00 00 00 00 03\n" + + "0010 01 00 03 03 00 00 00 08\n", + ("-s", "2905,2905,3"))))) + + def test_text2pcap_sctp_data(self): + '''Test SCTP DATA over IPv4''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 70}, + get_capinfos_cmp_info(check_capinfos_info(self, + self.run_text2pcap("0000: 01 00 03 03 00 00 00 08\n", + ("-S", "2905,2905,3"))))) + +class case_text2pcap_ipv4(subprocesstest.SubprocessTestCase): + '''Test TCP, UDP or SCTP header with -4 option''' + + def run_text2pcap_ipv4(self, content, args): + return run_text2pcap_content(self, content, ("-4", "127.0.0.1,127.0.0.1") + args) + + def test_text2pcap_ipv4_tcp(self): + '''Test TCP over IPv4''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 60}, + get_capinfos_cmp_info(check_capinfos_info(self, + self.run_text2pcap_ipv4("0000: ff ff ff ff\n", + ("-T", "1234,1234"))))) + + def test_text2pcap_ipv4_udp(self): + '''Test UDP over IPv4''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 60}, + get_capinfos_cmp_info(check_capinfos_info(self, + self.run_text2pcap_ipv4("0000: ff ff ff ff\n", + ("-u", "1234,1234"))))) + + def test_text2pcap_ipv4_sctp(self): + '''Test SCTP over IPv4''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 70}, + get_capinfos_cmp_info(check_capinfos_info(self, + self.run_text2pcap_ipv4( + "0000 00 03 00 18 00 00 00 00 00 00 00 00 00 00 00 03\n" + + "0010 01 00 03 03 00 00 00 08\n", + ("-s", "2905,2905,3"))))) + + def test_text2pcap_ipv4_sctp_data(self): + '''Test SCTP DATA over IPv4''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 70}, + get_capinfos_cmp_info(check_capinfos_info(self, + self.run_text2pcap_ipv4("0000: 01 00 03 03 00 00 00 08\n", + ("-S", "2905,2905,3"))))) + +class case_text2pcap_ipv6(subprocesstest.SubprocessTestCase): + '''Test TCP, UDP or SCTP header with -6 option''' + + def run_text2pcap_ipv6(self, content, args): + return run_text2pcap_content(self, content, ("-6", "::1,::1") + args) + + def test_text2pcap_ipv6_tcp(self): + '''Test TCP over IPv6''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 78}, + get_capinfos_cmp_info(check_capinfos_info(self, + self.run_text2pcap_ipv6("0000: ff ff ff ff\n", + ("-T", "1234,1234"))))) + + def test_text2pcap_ipv6_udp(self): + '''Test UDP over IPv6''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 66}, + get_capinfos_cmp_info(check_capinfos_info(self, + self.run_text2pcap_ipv6("0000: ff ff ff ff\n", + ("-u", "1234,1234"))))) + + def test_text2pcap_ipv6_sctp(self): + '''Test SCTP over IPv6''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 90}, + get_capinfos_cmp_info(check_capinfos_info(self, + self.run_text2pcap_ipv6( + "0000 00 03 00 18 00 00 00 00 00 00 00 00 00 00 00 03\n" + + "0010 01 00 03 03 00 00 00 08\n", + ("-s", "2905,2905,3"))))) + + def test_text2pcap_ipv6_sctp_data(self): + '''Test SCTP DATA over IPv6''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 90}, + get_capinfos_cmp_info(check_capinfos_info(self, + self.run_text2pcap_ipv6("0000: 01 00 03 03 00 00 00 08\n", + ("-S", "2905,2905,3"))))) diff --git a/text2pcap.c b/text2pcap.c index c5f795fb8c..0a189ab8be 100644 --- a/text2pcap.c +++ b/text2pcap.c @@ -1564,7 +1564,6 @@ parse_options (int argc, char *argv[]) return EXIT_FAILURE; } - hdr_ip = TRUE; hdr_ip_proto = 132; hdr_ethernet = TRUE; hdr_ethernet_proto = 0x800; @@ -1607,7 +1606,6 @@ parse_options (int argc, char *argv[]) return EXIT_FAILURE; } - hdr_ip = TRUE; hdr_ip_proto = 132; hdr_ethernet = TRUE; hdr_ethernet_proto = 0x800; @@ -1641,7 +1639,6 @@ parse_options (int argc, char *argv[]) print_usage(stderr); return EXIT_FAILURE; } - hdr_ip = TRUE; hdr_ip_proto = 17; hdr_ethernet = TRUE; hdr_ethernet_proto = 0x800; @@ -1671,7 +1668,6 @@ parse_options (int argc, char *argv[]) print_usage(stderr); return EXIT_FAILURE; } - hdr_ip = TRUE; hdr_ip_proto = 6; hdr_ethernet = TRUE; hdr_ethernet_proto = 0x800; @@ -1836,6 +1832,14 @@ parse_options (int argc, char *argv[]) timecode_default = *now_tm; timecode_default.tm_isdst = -1; /* Unknown for now, depends on time given to the strptime() function */ + if ((hdr_tcp || hdr_udp || hdr_sctp) && !(hdr_ip || hdr_ipv6)) { + /* + * If TCP (-T), UDP (-u) or SCTP (-s/-S) header options are specified + * but none of IPv4 (-4) or IPv6 (-6) options then add an IPv4 header + */ + hdr_ip = TRUE; + } + /* Display summary of our state */ if (!quiet) { fprintf(stderr, "Input from: %s\n", input_filename); @@ -1846,6 +1850,8 @@ parse_options (int argc, char *argv[]) hdr_ethernet_proto); if (hdr_ip) fprintf(stderr, "Generate dummy IP header: Protocol: %ld\n", hdr_ip_proto); + if (hdr_ipv6) fprintf(stderr, "Generate dummy IPv6 header: Protocol: %ld\n", + hdr_ip_proto); if (hdr_udp) fprintf(stderr, "Generate dummy UDP header: Source port: %u. Dest port: %u\n", hdr_src_port, hdr_dest_port); if (hdr_tcp) fprintf(stderr, "Generate dummy TCP header: Source port: %u. Dest port: %u\n",