From 6353dc967c8fe04edb519d8f3583e505b3b2b348 Mon Sep 17 00:00:00 2001
From: Michael Tuexen <tuexen@fh-muenster.de>
Date: Thu, 29 Jun 2017 21:55:01 +0200
Subject: [PATCH] Make encapsualted a per packet property.

This allows to support a mixture of encapsulated and non-encapsulated
packets in a single script file.

Sponsored by:	Netflix, Inc.
---
 gtests/net/packetdrill/packet.h               |  1 +
 gtests/net/packetdrill/packet_checksum.c      | 26 +++----
 gtests/net/packetdrill/packet_checksum.h      |  2 +-
 gtests/net/packetdrill/packet_parser.c        |  8 ++-
 gtests/net/packetdrill/packet_parser_test.c   |  4 +-
 gtests/net/packetdrill/packet_to_string.c     |  8 +--
 gtests/net/packetdrill/packet_to_string.h     |  2 +-
 .../net/packetdrill/packet_to_string_test.c   | 71 ++++++++-----------
 gtests/net/packetdrill/run_packet.c           | 35 +++++----
 gtests/net/packetdrill/sctp_packet.c          |  5 +-
 gtests/net/packetdrill/tcp_packet.c           |  2 +-
 11 files changed, 80 insertions(+), 84 deletions(-)

diff --git a/gtests/net/packetdrill/packet.h b/gtests/net/packetdrill/packet.h
index db09a339..fa7416c2 100644
--- a/gtests/net/packetdrill/packet.h
+++ b/gtests/net/packetdrill/packet.h
@@ -112,6 +112,7 @@ struct packet {
 #define FLAGS_SCTP_BAD_CRC32C     0x8  /* compute bad CRC32C for SCTP packets */
 #define FLAGS_SCTP_EXPLICIT_TAG   0x10 /* verification tag specified */
 #define FLAGS_SCTP_GENERIC_PACKET 0x20 /* set if it is a generic packet */
+#define FLAGS_UDP_ENCAPSULATED    0x40 /* TCP/UDP or SCTP/UDP encapsulated */
 
 	enum ip_ecn_t ecn;	/* IPv4/IPv6 ECN treatment for packet */
 
diff --git a/gtests/net/packetdrill/packet_checksum.c b/gtests/net/packetdrill/packet_checksum.c
index a5853e28..9bb72a9b 100644
--- a/gtests/net/packetdrill/packet_checksum.c
+++ b/gtests/net/packetdrill/packet_checksum.c
@@ -31,7 +31,7 @@
 #include "ipv6.h"
 #include "tcp.h"
 
-static void checksum_ipv4_packet(struct packet *packet, u8 udp_encaps)
+static void checksum_ipv4_packet(struct packet *packet)
 {
 	struct ipv4 *ipv4 = packet->ipv4;
 
@@ -50,7 +50,7 @@ static void checksum_ipv4_packet(struct packet *packet, u8 udp_encaps)
 		struct udp *udp;
 
 		sctp->crc32c = htonl(0);
-		if (udp_encaps == IPPROTO_SCTP) {
+		if (packet->flags & FLAGS_UDP_ENCAPSULATED) {
 			sctp->crc32c = sctp_crc32c(sctp, l4_bytes - sizeof(struct udp));
 		} else {
 			sctp->crc32c = sctp_crc32c(sctp, l4_bytes);
@@ -58,7 +58,7 @@ static void checksum_ipv4_packet(struct packet *packet, u8 udp_encaps)
 		if (packet->flags & FLAGS_SCTP_BAD_CRC32C) {
 			sctp->crc32c = htonl(ntohl(sctp->crc32c) + 1);
 		}
-		if (udp_encaps == IPPROTO_SCTP) {
+		if (packet->flags & FLAGS_UDP_ENCAPSULATED) {
 			udp = ((struct udp *)sctp) - 1;
 			udp->check = 0;
 			udp->check = tcp_udp_v4_checksum(ipv4->src_ip,
@@ -71,7 +71,7 @@ static void checksum_ipv4_packet(struct packet *packet, u8 udp_encaps)
 		struct udp *udp;
 
 		tcp->check = 0;
-		if (udp_encaps == IPPROTO_TCP) {
+		if (packet->flags & FLAGS_UDP_ENCAPSULATED) {
 			tcp->check = tcp_udp_v4_checksum(ipv4->src_ip,
 							 ipv4->dst_ip,
 							 IPPROTO_TCP, tcp,
@@ -82,7 +82,7 @@ static void checksum_ipv4_packet(struct packet *packet, u8 udp_encaps)
 							 IPPROTO_TCP, tcp,
 							 l4_bytes);
 		}
-		if (udp_encaps == IPPROTO_TCP) {
+		if (packet->flags & FLAGS_UDP_ENCAPSULATED) {
 			udp = ((struct udp *)tcp) - 1;
 			udp->check = 0;
 			udp->check = tcp_udp_v4_checksum(ipv4->src_ip,
@@ -120,7 +120,7 @@ static void checksum_ipv4_packet(struct packet *packet, u8 udp_encaps)
 	}
 }
 
-static void checksum_ipv6_packet(struct packet *packet, u8 udp_encaps)
+static void checksum_ipv6_packet(struct packet *packet)
 {
 	struct ipv6 *ipv6 = packet->ipv6;
 
@@ -138,7 +138,7 @@ static void checksum_ipv6_packet(struct packet *packet, u8 udp_encaps)
 		struct udp *udp;
 
 		sctp->crc32c = htonl(0);
-		if (udp_encaps == IPPROTO_SCTP) {
+		if (packet->flags & FLAGS_UDP_ENCAPSULATED) {
 			sctp->crc32c = sctp_crc32c(sctp, l4_bytes - sizeof(struct udp));
 		} else {
 			sctp->crc32c = sctp_crc32c(sctp, l4_bytes);
@@ -146,7 +146,7 @@ static void checksum_ipv6_packet(struct packet *packet, u8 udp_encaps)
 		if (packet->flags & FLAGS_SCTP_BAD_CRC32C) {
 			sctp->crc32c = htonl(ntohl(sctp->crc32c) + 1);
 		}
-		if (udp_encaps == IPPROTO_SCTP) {
+		if (packet->flags & FLAGS_UDP_ENCAPSULATED) {
 			udp = ((struct udp *)sctp) - 1;
 			udp->check = 0;
 			udp->check = tcp_udp_v6_checksum(&ipv6->src_ip,
@@ -159,7 +159,7 @@ static void checksum_ipv6_packet(struct packet *packet, u8 udp_encaps)
 		struct udp *udp;
 
 		tcp->check = 0;
-		if (udp_encaps == IPPROTO_TCP) {
+		if (packet->flags & FLAGS_UDP_ENCAPSULATED) {
 			tcp->check = tcp_udp_v6_checksum(&ipv6->src_ip,
 							 &ipv6->dst_ip,
 							 IPPROTO_TCP, tcp,
@@ -170,7 +170,7 @@ static void checksum_ipv6_packet(struct packet *packet, u8 udp_encaps)
 							 IPPROTO_TCP, tcp,
 							 l4_bytes);
 		}
-		if (udp_encaps == IPPROTO_TCP) {
+		if (packet->flags & FLAGS_UDP_ENCAPSULATED) {
 			udp = ((struct udp *)tcp) - 1;
 			udp->check = 0;
 			udp->check = tcp_udp_v6_checksum(&ipv6->src_ip,
@@ -211,13 +211,13 @@ static void checksum_ipv6_packet(struct packet *packet, u8 udp_encaps)
 	}
 }
 
-void checksum_packet(struct packet *packet, u8 udp_encaps)
+void checksum_packet(struct packet *packet)
 {
 	int address_family = packet_address_family(packet);
 	if (address_family == AF_INET)
-		return checksum_ipv4_packet(packet, udp_encaps);
+		return checksum_ipv4_packet(packet);
 	else if (address_family == AF_INET6)
-		return checksum_ipv6_packet(packet, udp_encaps);
+		return checksum_ipv6_packet(packet);
 	else
 		assert(!"bad ip version");
 }
diff --git a/gtests/net/packetdrill/packet_checksum.h b/gtests/net/packetdrill/packet_checksum.h
index 2eee17c3..2c87df3e 100644
--- a/gtests/net/packetdrill/packet_checksum.h
+++ b/gtests/net/packetdrill/packet_checksum.h
@@ -28,6 +28,6 @@
 #include "packet.h"
 
 /* Fill in layer 3 and layer 4 checksums for the given input 'packet'. */
-extern void checksum_packet(struct packet *packet, u8 udp_encaps);
+extern void checksum_packet(struct packet *packet);
 
 #endif /* __PACKET_CHECKSUM_H__ */
diff --git a/gtests/net/packetdrill/packet_parser.c b/gtests/net/packetdrill/packet_parser.c
index 008b01d7..f5ac7a5a 100644
--- a/gtests/net/packetdrill/packet_parser.c
+++ b/gtests/net/packetdrill/packet_parser.c
@@ -464,15 +464,17 @@ static int parse_udp(struct packet *packet, u8 udp_encaps,
 
 	DEBUGP("UDP src port: %d\n", ntohs(udp->src_port));
 	DEBUGP("UDP dst port: %d\n", ntohs(udp->dst_port));
-	if (udp_encaps == IPPROTO_SCTP)
+	if (udp_encaps == IPPROTO_SCTP) {
+		packet->flags |= FLAGS_UDP_ENCAPSULATED;
 		return parse_sctp(packet, p + udp_header_len,
 				  layer4_bytes - udp_header_len,
 				  packet_end, error);
-	else if (udp_encaps == IPPROTO_TCP)
+	} else if (udp_encaps == IPPROTO_TCP) {
+		packet->flags |= FLAGS_UDP_ENCAPSULATED;
 		return parse_tcp(packet, p + udp_header_len,
 				 layer4_bytes - udp_header_len,
 				  packet_end, error);
-	else {
+	} else {
 		assert(udp_encaps == 0);
 		packet->udp = udp;
 		p += layer4_bytes;
diff --git a/gtests/net/packetdrill/packet_parser_test.c b/gtests/net/packetdrill/packet_parser_test.c
index 5b216f07..46c8fca4 100644
--- a/gtests/net/packetdrill/packet_parser_test.c
+++ b/gtests/net/packetdrill/packet_parser_test.c
@@ -163,7 +163,7 @@ static void test_parse_sctp_udp_ipv4_packet(void)
 	assert(packet->icmpv6		== NULL);
 
 	assert(packet->time_usecs	== 0);
-	assert(packet->flags		== 0);
+	assert(packet->flags		== FLAGS_UDP_ENCAPSULATED);
 	assert(packet->ecn		== 0);
 
 	packet_free(packet);
@@ -213,7 +213,7 @@ static void test_parse_sctp_udp_ipv6_packet(void)
 	assert(packet->icmpv6		== NULL);
 
 	assert(packet->time_usecs	== 0);
-	assert(packet->flags		== 0);
+	assert(packet->flags		== FLAGS_UDP_ENCAPSULATED);
 	assert(packet->ecn		== 0);
 
 	packet_free(packet);
diff --git a/gtests/net/packetdrill/packet_to_string.c b/gtests/net/packetdrill/packet_to_string.c
index c74fb542..a9923760 100644
--- a/gtests/net/packetdrill/packet_to_string.c
+++ b/gtests/net/packetdrill/packet_to_string.c
@@ -310,7 +310,7 @@ static int encap_header_to_string(FILE *s, struct packet *packet, int layer,
 	return printer(s, packet, layer, format, error);
 }
 
-int packet_to_string(struct packet *packet, u8 udp_encaps,
+int packet_to_string(struct packet *packet,
 		     enum dump_format_t format,
 		     char **ascii_string, char **error)
 {
@@ -323,10 +323,10 @@ int packet_to_string(struct packet *packet, u8 udp_encaps,
 	int limit;
 
 	/* Print any encapsulation headers preceding layer 3 and 4 headers. */
-	if (udp_encaps == 0)
-		limit = header_count - 2;
-	else
+	if (packet->flags & FLAGS_UDP_ENCAPSULATED)
 		limit = header_count - 3;
+	else
+		limit = header_count - 2;
 	for (i = 0; i < limit; ++i) {
 		if (packet->headers[i].type == HEADER_NONE)
 			break;
diff --git a/gtests/net/packetdrill/packet_to_string.h b/gtests/net/packetdrill/packet_to_string.h
index 3a4fb40d..462a4f9e 100644
--- a/gtests/net/packetdrill/packet_to_string.h
+++ b/gtests/net/packetdrill/packet_to_string.h
@@ -37,7 +37,7 @@ enum dump_format_t {
  * packet 'packet'. Returns STATUS_OK on success; on failure returns
  * STATUS_ERR and sets error message.
  */
-extern int packet_to_string(struct packet *packet, u8 udp_encaps,
+extern int packet_to_string(struct packet *packet,
 			    enum dump_format_t format,
 			    char **ascii_string, char **error);
 
diff --git a/gtests/net/packetdrill/packet_to_string_test.c b/gtests/net/packetdrill/packet_to_string_test.c
index 2ad71b44..6f5084fd 100644
--- a/gtests/net/packetdrill/packet_to_string_test.c
+++ b/gtests/net/packetdrill/packet_to_string_test.c
@@ -68,7 +68,7 @@ static void test_sctp_ipv4_packet_to_string(void)
 	char *dump = NULL, *expected = NULL;
 
 	/* Test a DUMP_SHORT dump */
-	status = packet_to_string(packet, 0, DUMP_SHORT, &dump, &error);
+	status = packet_to_string(packet, DUMP_SHORT, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -78,7 +78,7 @@ static void test_sctp_ipv4_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_FULL dump */
-	status = packet_to_string(packet, 0, DUMP_FULL, &dump, &error);
+	status = packet_to_string(packet, DUMP_FULL, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -89,7 +89,7 @@ static void test_sctp_ipv4_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_VERBOSE dump */
-	status = packet_to_string(packet, 0, DUMP_VERBOSE, &dump, &error);
+	status = packet_to_string(packet, DUMP_VERBOSE, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -295,7 +295,7 @@ static void test_sctp_ipv6_packet_to_string(void)
 	char *dump = NULL, *expected = NULL;
 
 	/* Test a DUMP_SHORT dump */
-	status = packet_to_string(packet, 0, DUMP_SHORT, &dump, &error);
+	status = packet_to_string(packet, DUMP_SHORT, &dump, &error);
 #if DEBUG_LOGGING == 1
 	if (status != STATUS_OK) {
 		printf("error was: %s\n", error);
@@ -362,7 +362,7 @@ static void test_sctp_ipv6_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_FULL dump */
-	status = packet_to_string(packet, 0, DUMP_FULL, &dump, &error);
+	status = packet_to_string(packet, DUMP_FULL, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -425,7 +425,7 @@ static void test_sctp_ipv6_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_VERBOSE dump */
-	status = packet_to_string(packet, 0, DUMP_VERBOSE, &dump, &error);
+	status = packet_to_string(packet, DUMP_VERBOSE, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -567,7 +567,7 @@ static void test_sctp_udp_ipv4_packet_to_string(void)
 	char *dump = NULL, *expected = NULL;
 
 	/* Test a DUMP_SHORT dump */
-	status = packet_to_string(packet, IPPROTO_SCTP, DUMP_SHORT, &dump,
+	status = packet_to_string(packet, DUMP_SHORT, &dump,
 				  &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
@@ -578,7 +578,7 @@ static void test_sctp_udp_ipv4_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_FULL dump */
-	status = packet_to_string(packet, IPPROTO_SCTP, DUMP_FULL, &dump,
+	status = packet_to_string(packet, DUMP_FULL, &dump,
 				  &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
@@ -590,7 +590,7 @@ static void test_sctp_udp_ipv4_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_VERBOSE dump */
-	status = packet_to_string(packet, IPPROTO_SCTP, DUMP_VERBOSE, &dump,
+	status = packet_to_string(packet, DUMP_VERBOSE, &dump,
 				  &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
@@ -648,8 +648,7 @@ static void test_sctp_udp_ipv6_packet_to_string(void)
 	char *dump = NULL, *expected = NULL;
 
 	/* Test a DUMP_SHORT dump */
-	status = packet_to_string(packet, IPPROTO_SCTP, DUMP_SHORT, &dump,
-				  &error);
+	status = packet_to_string(packet, DUMP_SHORT, &dump, &error);
 #if DEBUG_LOGGING == 1
 	if (status != STATUS_OK) {
 		printf("error was: %s\n", error);
@@ -665,8 +664,7 @@ static void test_sctp_udp_ipv6_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_FULL dump */
-	status = packet_to_string(packet, IPPROTO_SCTP, DUMP_FULL, &dump,
-				  &error);
+	status = packet_to_string(packet, DUMP_FULL, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -677,8 +675,7 @@ static void test_sctp_udp_ipv6_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_VERBOSE dump */
-	status = packet_to_string(packet, IPPROTO_SCTP, DUMP_VERBOSE, &dump,
-				  &error);
+	status = packet_to_string(packet, DUMP_VERBOSE, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -739,8 +736,7 @@ static void test_tcp_udp_ipv4_packet_to_string(void)
 	char *dump = NULL, *expected = NULL;
 
 	/* Test a DUMP_SHORT dump */
-	status = packet_to_string(packet, IPPROTO_TCP, DUMP_SHORT, &dump,
-				  &error);
+	status = packet_to_string(packet, DUMP_SHORT, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -753,8 +749,7 @@ static void test_tcp_udp_ipv4_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_FULL dump */
-	status = packet_to_string(packet, IPPROTO_TCP, DUMP_FULL, &dump,
-				  &error);
+	status = packet_to_string(packet, DUMP_FULL, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -768,8 +763,7 @@ static void test_tcp_udp_ipv4_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_VERBOSE dump */
-	status = packet_to_string(packet, IPPROTO_TCP, DUMP_VERBOSE, &dump,
-				  &error);
+	status = packet_to_string(packet, DUMP_VERBOSE, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -837,8 +831,7 @@ static void test_tcp_udp_ipv6_packet_to_string(void)
 	char *dump = NULL, *expected = NULL;
 
 	/* Test a DUMP_SHORT dump */
-	status = packet_to_string(packet, IPPROTO_TCP, DUMP_SHORT, &dump,
-				  &error);
+	status = packet_to_string(packet, DUMP_SHORT, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -850,8 +843,7 @@ static void test_tcp_udp_ipv6_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_FULL dump */
-	status = packet_to_string(packet, IPPROTO_TCP, DUMP_FULL, &dump,
-				  &error);
+	status = packet_to_string(packet, DUMP_FULL, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -864,8 +856,7 @@ static void test_tcp_udp_ipv6_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_VERBOSE dump */
-	status = packet_to_string(packet, IPPROTO_TCP, DUMP_VERBOSE, &dump,
-				  &error);
+	status = packet_to_string(packet, DUMP_VERBOSE, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -924,7 +915,7 @@ static void test_tcp_ipv4_packet_to_string(void)
 	char *dump = NULL, *expected = NULL;
 
 	/* Test a DUMP_SHORT dump */
-	status = packet_to_string(packet, 0, DUMP_SHORT, &dump, &error);
+	status = packet_to_string(packet, DUMP_SHORT, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -936,7 +927,7 @@ static void test_tcp_ipv4_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_FULL dump */
-	status = packet_to_string(packet, 0, DUMP_FULL, &dump, &error);
+	status = packet_to_string(packet, DUMP_FULL, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -949,7 +940,7 @@ static void test_tcp_ipv4_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_VERBOSE dump */
-	status = packet_to_string(packet, 0, DUMP_VERBOSE, &dump, &error);
+	status = packet_to_string(packet, DUMP_VERBOSE, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -1009,7 +1000,7 @@ static void test_tcp_ipv6_packet_to_string(void)
 	char *dump = NULL, *expected = NULL;
 
 	/* Test a DUMP_SHORT dump */
-	status = packet_to_string(packet, 0, DUMP_SHORT, &dump, &error);
+	status = packet_to_string(packet, DUMP_SHORT, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -1020,7 +1011,7 @@ static void test_tcp_ipv6_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_FULL dump */
-	status = packet_to_string(packet, 0, DUMP_FULL, &dump, &error);
+	status = packet_to_string(packet, DUMP_FULL, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -1032,7 +1023,7 @@ static void test_tcp_ipv6_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_VERBOSE dump */
-	status = packet_to_string(packet, 0, DUMP_VERBOSE, &dump, &error);
+	status = packet_to_string(packet, DUMP_VERBOSE, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -1090,7 +1081,7 @@ static void test_gre_mpls_tcp_ipv4_packet_to_string(void)
 	char *dump = NULL, *expected = NULL;
 
 	/* Test a DUMP_FULL dump */
-	status = packet_to_string(packet, 0, DUMP_FULL, &dump, &error);
+	status = packet_to_string(packet, DUMP_FULL, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -1138,7 +1129,7 @@ static void test_udplite_ipv4_packet_to_string(void)
 	char *dump = NULL, *expected = NULL;
 
 	/* Test a DUMP_SHORT dump */
-	status = packet_to_string(packet, 0, DUMP_SHORT, &dump, &error);
+	status = packet_to_string(packet, DUMP_SHORT, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -1149,7 +1140,7 @@ static void test_udplite_ipv4_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_FULL dump */
-	status = packet_to_string(packet, 0, DUMP_FULL, &dump, &error);
+	status = packet_to_string(packet, DUMP_FULL, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -1161,7 +1152,7 @@ static void test_udplite_ipv4_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_VERBOSE dump */
-	status = packet_to_string(packet, 0, DUMP_VERBOSE, &dump, &error);
+	status = packet_to_string(packet, DUMP_VERBOSE, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -1216,7 +1207,7 @@ static void test_udplite_ipv6_packet_to_string(void)
 	char *dump = NULL, *expected = NULL;
 
 	/* Test a DUMP_SHORT dump */
-	status = packet_to_string(packet, 0, DUMP_SHORT, &dump, &error);
+	status = packet_to_string(packet, DUMP_SHORT, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -1227,7 +1218,7 @@ static void test_udplite_ipv6_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_FULL dump */
-	status = packet_to_string(packet, 0, DUMP_FULL, &dump, &error);
+	status = packet_to_string(packet, DUMP_FULL, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
@@ -1239,7 +1230,7 @@ static void test_udplite_ipv6_packet_to_string(void)
 	free(dump);
 
 	/* Test a DUMP_VERBOSE dump */
-	status = packet_to_string(packet, 0, DUMP_VERBOSE, &dump, &error);
+	status = packet_to_string(packet, DUMP_VERBOSE, &dump, &error);
 	assert(status == STATUS_OK);
 	assert(error == NULL);
 	printf("dump = '%s'\n", dump);
diff --git a/gtests/net/packetdrill/run_packet.c b/gtests/net/packetdrill/run_packet.c
index bcc4469c..350bbe4a 100644
--- a/gtests/net/packetdrill/run_packet.c
+++ b/gtests/net/packetdrill/run_packet.c
@@ -101,7 +101,7 @@ static u16 next_ephemeral_port(struct state *state)
  * *error followed by the given type and a hex dump of the given
  * packet.
  */
-static void add_packet_dump(struct state *state, char **error, const char *type,
+static void add_packet_dump(char **error, const char *type,
 			    struct packet *packet, s64 time_usecs,
 			    enum dump_format_t format)
 {
@@ -109,7 +109,7 @@ static void add_packet_dump(struct state *state, char **error, const char *type,
 		char *old_error = *error;
 		char *dump = NULL, *dump_error = NULL;
 
-		packet_to_string(packet, state->config->udp_encaps, format,
+		packet_to_string(packet, format,
 				 &dump, &dump_error);
 		asprintf(error, "%s\n%s packet: %9.6f %s%s%s",
 			 old_error, type, usecs_to_secs(time_usecs), dump,
@@ -129,8 +129,8 @@ static void verbose_packet_dump(struct state *state, const char *type,
 	if (state->config->verbose) {
 		char *dump = NULL, *dump_error = NULL;
 
-		packet_to_string(live_packet, state->config->udp_encaps,
-				 DUMP_SHORT, &dump, &dump_error);
+		packet_to_string(live_packet, DUMP_SHORT,
+				 &dump, &dump_error);
 
 		printf("%s packet: %9.6f %s%s%s\n",
 		       type, usecs_to_secs(time_usecs), dump,
@@ -2893,10 +2893,10 @@ static int verify_outbound_live_packet(
 	result = STATUS_OK;
 
 out:
-	add_packet_dump(state, error, "script", script_packet, script_usecs,
+	add_packet_dump(error, "script", script_packet, script_usecs,
 			DUMP_SHORT);
 	if (actual_packet != NULL) {
-		add_packet_dump(state, error, "actual", actual_packet,
+		add_packet_dump(error, "actual", actual_packet,
 				actual_usecs, DUMP_SHORT);
 		packet_free(actual_packet);
 	}
@@ -3177,7 +3177,7 @@ static int do_outbound_script_packet(
 	/* Save the TCP header so we can reset the connection at the end. */
 	if (live_packet->tcp) {
 		socket->last_outbound_tcp_header = *(live_packet->tcp);
-		if (state->config->udp_encaps == IPPROTO_TCP) {
+		if (live_packet->flags & FLAGS_UDP_ENCAPSULATED) {
 			struct udp *udp = (struct udp *)(live_packet->tcp) - 1;
 
 			socket->last_outbound_udp_encaps_src_port = ntohs(udp->src_port);
@@ -3186,7 +3186,7 @@ static int do_outbound_script_packet(
 	}
 
 	if (live_packet->sctp) {
-		if (state->config->udp_encaps == IPPROTO_SCTP) {
+		if (live_packet->flags & FLAGS_UDP_ENCAPSULATED) {
 			struct udp *udp = (struct udp *)(live_packet->sctp) - 1;
 
 			socket->last_outbound_udp_encaps_src_port = ntohs(udp->src_port);
@@ -3206,8 +3206,7 @@ out:
 
 /* Checksum the packet and inject it into the kernel under test. */
 static int send_live_ip_packet(struct netdev *netdev,
-			       struct packet *packet,
-			       u8 udp_encaps)
+			       struct packet *packet)
 {
 	assert(packet->ip_bytes > 0);
 	/* We do IPv4 and IPv6 */
@@ -3217,7 +3216,7 @@ static int send_live_ip_packet(struct netdev *netdev,
 	       packet->icmpv4 || packet->icmpv6);
 
 	/* Fill in layer 3 and layer 4 checksums */
-	checksum_packet(packet, udp_encaps);
+	checksum_packet(packet);
 
 	return netdev_send(netdev, packet);
 }
@@ -3290,7 +3289,7 @@ static int do_inbound_script_packet(
 								break;
 							}
 						}
-						if (state->config->udp_encaps == IPPROTO_SCTP) {
+						if (packet->flags & FLAGS_UDP_ENCAPSULATED) {
 							struct udp *udp;
 
 							assert(packet->headers[i + 1].type == HEADER_UDP);
@@ -3339,7 +3338,7 @@ static int do_inbound_script_packet(
 								break;
 							}
 						}
-						if (state->config->udp_encaps == IPPROTO_SCTP) {
+						if (packet->flags & FLAGS_UDP_ENCAPSULATED) {
 							struct udp *udp;
 
 							assert(packet->headers[i + 1].type == HEADER_UDP);
@@ -3381,7 +3380,7 @@ static int do_inbound_script_packet(
 		socket->last_injected_tcp_header = *(live_packet->tcp);
 		socket->last_injected_tcp_payload_len =
 			packet_payload_len(live_packet);
-		if (state->config->udp_encaps == IPPROTO_TCP) {
+		if (live_packet->flags & FLAGS_UDP_ENCAPSULATED) {
 			struct udp *udp = (struct udp *)(live_packet->tcp) - 1;
 
 			socket->last_injected_udp_encaps_src_port = ntohs(udp->src_port);
@@ -3389,7 +3388,7 @@ static int do_inbound_script_packet(
 		}
 	}
 	if (live_packet->sctp) {
-		if (state->config->udp_encaps == IPPROTO_SCTP) {
+		if (live_packet->flags & FLAGS_UDP_ENCAPSULATED) {
 			struct udp *udp = (struct udp *)(live_packet->sctp) - 1;
 
 			socket->last_injected_udp_encaps_src_port = ntohs(udp->src_port);
@@ -3411,7 +3410,7 @@ static int do_inbound_script_packet(
 	}
 
 	/* Inject live packet into kernel. */
-	result = send_live_ip_packet(state->netdev, live_packet, state->config->udp_encaps);
+	result = send_live_ip_packet(state->netdev, live_packet);
 
 out:
 	packet_free(live_packet);
@@ -3532,7 +3531,7 @@ int reset_connection(struct state *state, struct socket *socket)
 	set_packet_tuple(packet, &live_inbound, state->config->udp_encaps != 0);
 
 	/* Inject live packet into kernel. */
-	result = send_live_ip_packet(state->netdev, packet, state->config->udp_encaps);
+	result = send_live_ip_packet(state->netdev, packet);
 
 	packet_free(packet);
 
@@ -3595,7 +3594,7 @@ int abort_association(struct state *state, struct socket *socket)
 	}
 
 	/* Inject live packet into kernel. */
-	result = send_live_ip_packet(state->netdev, packet, state->config->udp_encaps);
+	result = send_live_ip_packet(state->netdev, packet);
 
 	packet_free(packet);
 
diff --git a/gtests/net/packetdrill/sctp_packet.c b/gtests/net/packetdrill/sctp_packet.c
index 4237d9a9..62802805 100644
--- a/gtests/net/packetdrill/sctp_packet.c
+++ b/gtests/net/packetdrill/sctp_packet.c
@@ -3392,7 +3392,7 @@ new_sctp_packet(int address_family,
 	packet = packet_new(overbook ? MAX_SCTP_DATAGRAM_BYTES : ip_bytes);
 	memset(packet->buffer, 0, overbook ? MAX_SCTP_DATAGRAM_BYTES : ip_bytes);
 	packet->direction = direction;
-	packet->flags = 0;
+	packet->flags = encapsulate ? FLAGS_UDP_ENCAPSULATED : 0;
 	if (bad_crc32c) {
 		packet->flags |= FLAGS_SCTP_BAD_CRC32C;
 	}
@@ -3539,6 +3539,9 @@ new_sctp_generic_packet(int address_family,
 	if (tag != -1) {
 		packet->flags |= FLAGS_SCTP_EXPLICIT_TAG;
 	}
+	if (encapsulate) {
+		packet->flags |= FLAGS_SCTP_BAD_CRC32C;
+	}
 	packet->ecn = ecn;
 
 	/* Set IP header fields */
diff --git a/gtests/net/packetdrill/tcp_packet.c b/gtests/net/packetdrill/tcp_packet.c
index fd536547..e83a8066 100644
--- a/gtests/net/packetdrill/tcp_packet.c
+++ b/gtests/net/packetdrill/tcp_packet.c
@@ -115,7 +115,7 @@ struct packet *new_tcp_packet(int address_family,
 	memset(packet->buffer, 0, ip_bytes);
 
 	packet->direction = direction;
-	packet->flags = 0;
+	packet->flags = encapsulate ? FLAGS_UDP_ENCAPSULATED : 0;
 	packet->ecn = ecn;
 
 	/* Set IP header fields */
-- 
GitLab