Lines Matching +full:ipv4 +full:- +full:no +full:- +full:config +full:- +full:for +full:- +full:cpp
8 * http://www.apache.org/licenses/LICENSE-2.0
13 * See the License for the specific language governing permissions and
16 * clatd_test.cpp - unit tests for clatd
33 #include "config.h"
40 // For convenience.
50 // clang-format off
77 0xc8, 0x8b, 0, 53, /* Port 51339->53 */ \
78 0x00, UDP_LEN, 0, 0, /* Length 21, checksum empty for now */
90 // Macros to return pseudo-headers from packets.
175 // clang-format on
190 return ntohs(ip->frag_off) & (IP_OFFMASK | IP_MF); in is_ipv4_fragment()
194 if (ip6->ip6_nxt != IPPROTO_FRAGMENT) { in is_ipv6_fragment()
199 (frag->ip6f_offlg & (IP6F_OFF_MASK | IP6F_MORE_FRAG)); in is_ipv6_fragment()
203 return ntohs(ip->frag_off) & IP_OFFMASK; in ipv4_fragment_offset()
207 return ntohs((frag->ip6f_offlg & IP6F_OFF_MASK) >> 3); in ipv6_fragment_offset()
219 ASSERT_GE(len, sizeof(*ip)) << msg << ": IPv4 packet shorter than IPv4 header\n"; in check_packet()
220 EXPECT_EQ(5, ip->ihl) << msg << ": Unsupported IP header length\n"; in check_packet()
221 EXPECT_EQ(len, ntohs(ip->tot_len)) << msg << ": Incorrect IPv4 length\n"; in check_packet()
223 protocol = ip->protocol; in check_packet()
226 payload_length = len - sizeof(*ip); in check_packet()
230 << msg << ": Unsupported IPv4 protocol " << protocol << "\n"; in check_packet()
236 EXPECT_EQ(len - sizeof(*ip6), htons(ip6->ip6_plen)) << msg << ": Incorrect IPv6 length\n"; in check_packet()
238 if (ip6->ip6_nxt == IPPROTO_FRAGMENT) { in check_packet()
242 protocol = frag->ip6f_nxt; in check_packet()
246 payload_length = len - sizeof(*ip6) - sizeof(*frag); in check_packet()
249 // Since there are no extension headers except Fragment, this must be the payload. in check_packet()
250 protocol = ip6->ip6_nxt; in check_packet()
252 payload_length = len - sizeof(*ip6); in check_packet()
287 EXPECT_NE(0, udp->check) << msg << ": UDP checksum 0 should be 0xffff"; in check_packet()
290 EXPECT_EQ(payload_length, ntohs(udp->len)) << msg << ": Incorrect UDP length\n"; in check_packet()
303 for (int i = 0; i < numpackets; i++) { in reassemble_packet()
316 << msg << ": IPv4 fragment #" << i + 1 << " not a fragment\n"; in reassemble_packet()
318 << msg << ": IPv4 fragment #" << i + 1 << ": inconsistent offset\n"; in reassemble_packet()
339 protocol = frag->ip6f_nxt; in reassemble_packet()
356 int payload_length = len - payload_offset; in reassemble_packet()
363 // Fix up the reassembled headers to reflect fragmentation and length (and IPv4 checksum). in reassemble_packet()
366 ip->frag_off &= ~htons(IP_MF); in reassemble_packet()
367 ip->tot_len = htons(total_length); in reassemble_packet()
368 ip->check = 0; in reassemble_packet()
369 ip->check = ip_checksum(ip, sizeof(*ip)); in reassemble_packet()
370 ASSERT_FALSE(is_ipv4_fragment(ip)) << msg << ": reassembled IPv4 packet is a fragment!\n"; in reassemble_packet()
373 ip6->ip6_nxt = protocol; in reassemble_packet()
374 ip6->ip6_plen = htons(total_length - sizeof(*ip6)); in reassemble_packet()
375 ASSERT_FALSE(is_ipv6_fragment(ip6, ip6->ip6_plen)) in reassemble_packet()
388 for (unsigned i = 0; i < len; i++) { in check_data_matches()
390 snprintf(expected_hexdump + pos, hexdump_len - pos, "\n "); in check_data_matches()
391 snprintf(actual_hexdump + pos, hexdump_len - pos, "\n "); in check_data_matches()
394 snprintf(expected_hexdump + pos, hexdump_len - pos, " %02x", ((uint8_t *)expected)[i]); in check_data_matches()
395 snprintf(actual_hexdump + pos, hexdump_len - pos, " %02x", ((uint8_t *)actual)[i]); in check_data_matches()
412 pseudo_checksum = ipv4_pseudo_header_checksum(ip, ntohs(udp->len)); in fix_udp_checksum()
418 pseudo_checksum = ipv6_pseudo_header_checksum(ip6, ntohs(udp->len), IPPROTO_UDP); in fix_udp_checksum()
426 udp->check = 0; in fix_udp_checksum()
427 udp->check = ip_checksum_finish(ip_checksum_add(pseudo_checksum, udp, ntohs(udp->len))); in fix_udp_checksum()
430 // Testing stub for send_rawv6. The real version uses sendmsg() with a
469 // Translating to IPv4. Expect a tun header. in do_translate_packet()
480 *outlen = len - sizeof(new_tun_header); in do_translate_packet()
505 for (int i = 0; i < numfragments; i++) { in check_fragment_translation()
536 return -1; in get_transport_checksum()
538 protocol = ip->protocol; in get_transport_checksum()
543 protocol = ip6->ip6_nxt; in get_transport_checksum()
547 return -1; in get_transport_checksum()
552 return ((struct udphdr *)payload)->check; in get_transport_checksum()
555 return ((struct tcphdr *)payload)->check; in get_transport_checksum()
559 return -1; in get_transport_checksum()
564 // Create some fake but realistic-looking sockets so update_clat_ipv6_address doesn't balk. in makeTunData()
573 close(tunnel->write_fd6); in freeTunData()
574 close(tunnel->read_fd6); in freeTunData()
575 close(tunnel->fd4); in freeTunData()
632 for (size_t pos = 0; pos < size; pos++) { in count_onebits()
634 for (int shift = 0; shift < 8; shift++) { in count_onebits()
675 for (int i = 0; i < 100000; i++) { in TEST_F()
691 // Check that the IID is checksum-neutral with the NAT64 prefix and the in TEST_F()
707 << " not checksum-neutral with " << ipv4_str << " and " << plat_str in TEST_F()
709 << "\n IPv4 checksum: " << c1 in TEST_F()
736 // If no addresses are free, return INADDR_NONE. in TEST_F()
782 // Create an interface for configure_tun_ip to configure and bring up. in TEST_F()
792 EXPECT_EQ(inet_addr("192.0.0.6"), ip->ip4.s_addr); in TEST_F()
806 // Create an interface for configure_tun_ip to configure and bring up. in TEST_F()
817 EXPECT_EQ(inet_addr("192.0.2.1"), ip->ip4.s_addr); in TEST_F()
827 ASSERT_EQ(sizeof(struct iphdr), sizeof(v4_header)) << "Test IPv4 header: incorrect length\n"; in TEST_F()
840 ASSERT_EQ(kUdpV4Checksum, udp->check) << "UDP/IPv4 packet checksum sanity check\n"; in TEST_F()
841 check_packet(v4_udp_packet, sizeof(v4_udp_packet), "UDP/IPv4 packet sanity check"); in TEST_F()
846 ASSERT_EQ(kUdpV6Checksum, udp->check) << "UDP/IPv6 packet checksum sanity check\n"; in TEST_F()
850 check_packet(ipv4_ping, sizeof(ipv4_ping), "IPv4 ping sanity check"); in TEST_F()
860 check_packet(reassembled, total_length, "IPv4 Reassembled packet is valid"); in TEST_F()
861 ASSERT_EQ(sizeof(kReassembledIPv4), total_length) << "IPv4 reassembly sanity check: length\n"; in TEST_F()
864 check_data_matches(kReassembledIPv4, reassembled, total_length, "IPv4 reassembly sanity check"); in TEST_F()
906 EXPECT_EQ(0x3ad0U, ipv4_pseudo_sum) << "IPv4 pseudo-checksum sanity check\n"; in TEST_F()
907 EXPECT_EQ(0x2644bU, ipv6_pseudo_sum) << "IPv6 pseudo-checksum sanity check\n"; in TEST_F()
911 << "Unexpected UDP/IPv4 checksum\n"; in TEST_F()
919 << "Adjust IPv4/UDP checksum to IPv6\n"; in TEST_F()
922 << "Adjust IPv6/UDP checksum to IPv4\n"; in TEST_F()
940 for (i = 0; i < ARRAYSIZE(DATA); i++) { in TEST_F()
942 uint16_t result = ip_checksum_adjust(data->checksum, data->old_hdr_sum, data->new_hdr_sum); in TEST_F()
943 EXPECT_EQ(result, data->result) in TEST_F()
945 << "\n Expected: " << data->result in TEST_F()
947 << "\n checksum=" << data->checksum in TEST_F()
948 << " old_sum=" << data->old_hdr_sum << " new_sum=" << data->new_hdr_sum << "\n"; in TEST_F()
961 "UDP/IPv4 -> UDP/IPv6 translation"); in TEST_F()
963 "UDP/IPv6 -> UDP/IPv4 translation"); in TEST_F()
968 "ICMP->ICMPv6 translation"); in TEST_F()
970 "ICMPv6->ICMP translation"); in TEST_F()
978 ARRAYSIZE(kIPv4Fragments), "IPv4->IPv6 fragment translation"); in TEST_F()
981 ARRAYSIZE(kIPv6Fragments), "IPv6->IPv4 fragment translation"); in TEST_F()
990 // do_translate_packet already checks packets for validity and verifies the checksum. in check_translate_checksum_neutral()
993 ASSERT_NE(-1, original_check); in check_translate_checksum_neutral()
994 ASSERT_NE(-1, translated_check); in check_translate_checksum_neutral()
1000 // Generate a random clat IPv6 address and check that translation is checksum-neutral. in TEST_F()
1008 // Check that translating UDP packets is checksum-neutral. First, IPv4. in TEST_F()
1012 "UDP/IPv4 -> UDP/IPv6 checksum neutral"); in TEST_F()
1018 memcpy(&ip6->ip6_src, &Global_Clatd_Config.ipv6_local_subnet, sizeof(ip6->ip6_src)); in TEST_F()
1021 "UDP/IPv4 -> UDP/IPv6 checksum neutral"); in TEST_F()
1028 in6_addr actual = ip->ip6; in TEST_F()
1034 // because there is no way to fetch it from the kernel. in expectSocketBound()
1078 // because there is no way to fetch it from the kernel. in TEST_F()