test: add imitate_tcp_packet test case (TCP over GREv0: GRE enable checksum)

This commit is contained in:
luwenpeng
2024-07-15 15:07:38 +08:00
parent d31cfd02fd
commit 07ce636f64
13 changed files with 408 additions and 150 deletions

View File

@@ -41,10 +41,10 @@ uint16_t packet_get_payload_len(const struct packet *pkt);
* tcp_ack: the acknowledgment number of the new TCP packet (in host byte order) * tcp_ack: the acknowledgment number of the new TCP packet (in host byte order)
* tcp_options_len: the length of the options (must be a multiple of 4) * tcp_options_len: the length of the options (must be a multiple of 4)
*/ */
struct packet *imitate_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, struct packet *craft_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags,
const char *tcp_options, uint16_t tcp_options_len, const char *tcp_options, uint16_t tcp_options_len,
const char *tcp_payload, uint16_t tcp_payload_len); const char *tcp_payload, uint16_t tcp_payload_len);
struct packet *imitate_udp_packet(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len); struct packet *craft_udp_packet(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len);
struct packet *craft_packet_from_scratch(const struct layer larers[], uint16_t layer_count, const char *payload, uint16_t payload_len); struct packet *craft_packet_from_scratch(const struct layer larers[], uint16_t layer_count, const char *payload, uint16_t payload_len);
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -723,7 +723,7 @@ static struct packet *ip_frag_reassemble(struct ip_reassembly *assy, struct ip_f
ip4_hdr_set_mf_flag(ip4_hdr, false); // update more fragment flag ip4_hdr_set_mf_flag(ip4_hdr, false); // update more fragment flag
ip4_hdr_set_frag_offset(ip4_hdr, 0); // update fragment offset ip4_hdr_set_frag_offset(ip4_hdr, 0); // update fragment offset
ip4_hdr->ip_sum = 0; // update checksum ip4_hdr->ip_sum = 0; // update checksum
ip4_hdr->ip_sum = checksum((const char *)ip4_hdr, flow->hdr.l3_len); ip4_hdr->ip_sum = checksum((const void *)ip4_hdr, flow->hdr.l3_len);
} }
else else
{ {

View File

@@ -1,4 +1,4 @@
add_library(packet packet_parse.cpp packet_build.cpp packet_dump.cpp packet_utils.cpp packet_ldbc.cpp packet_layer.cpp packet_tunnel.cpp checksum.cpp) add_library(packet packet_parse.cpp packet_craft.cpp packet_dump.cpp packet_utils.cpp packet_ldbc.cpp packet_layer.cpp packet_tunnel.cpp checksum.cpp)
target_include_directories(packet PUBLIC ${CMAKE_CURRENT_LIST_DIR}) target_include_directories(packet PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/deps/uthash) target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/deps/uthash)
target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/include) target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/include)

View File

@@ -2,27 +2,22 @@
#include <arpa/inet.h> #include <arpa/inet.h>
uint16_t checksum(const char *data, uint16_t len) uint16_t checksum(const void *data, int len)
{ {
uint16_t *ptr = (uint16_t *)data;
uint32_t sum = 0; uint32_t sum = 0;
const uint16_t *ip1 = (const uint16_t *)data;
while (len > 1) while (len > 1)
{ {
sum += *ip1++; sum += *ptr++;
if (sum & 0x80000000)
{
sum = (sum & 0xFFFF) + (sum >> 16);
}
len -= 2; len -= 2;
} }
if (len == 1)
while (sum >> 16)
{ {
sum = (sum & 0xFFFF) + (sum >> 16); sum += *(uint8_t *)ptr;
} }
sum = (sum >> 16) + (sum & 0xFFFF);
return (~sum); sum += (sum >> 16);
return (uint16_t)~sum;
} }
uint16_t checksum_v4(const void *l4_hdr_ptr, uint16_t l4_total_len, uint8_t l4_proto, struct in_addr *src_addr, struct in_addr *dst_addr) uint16_t checksum_v4(const void *l4_hdr_ptr, uint16_t l4_total_len, uint8_t l4_proto, struct in_addr *src_addr, struct in_addr *dst_addr)

View File

@@ -7,7 +7,7 @@ extern "C"
#include <stdint.h> #include <stdint.h>
uint16_t checksum(const char *data, uint16_t len); uint16_t checksum(const void *data, int len);
uint16_t checksum_v4(const void *l4_hdr_ptr, uint16_t l4_total_len, uint8_t l4_proto, struct in_addr *src_addr, struct in_addr *dst_addr); uint16_t checksum_v4(const void *l4_hdr_ptr, uint16_t l4_total_len, uint8_t l4_proto, struct in_addr *src_addr, struct in_addr *dst_addr);
uint16_t checksum_v6(const void *l4_hdr_ptr, uint16_t l4_total_len, uint8_t l4_proto, struct in6_addr *src_addr, struct in6_addr *dst_addr); uint16_t checksum_v6(const void *l4_hdr_ptr, uint16_t l4_total_len, uint8_t l4_proto, struct in6_addr *src_addr, struct in6_addr *dst_addr);

View File

@@ -398,7 +398,7 @@ static inline int gre0_hdr_to_str(const struct gre0_hdr *hdr, char *buf, size_t
gre0_hdr_get_seq_flag(hdr), gre0_hdr_get_strict_flag(hdr), gre0_hdr_get_version(hdr), eth_proto_to_str(proto)); gre0_hdr_get_seq_flag(hdr), gre0_hdr_get_strict_flag(hdr), gre0_hdr_get_version(hdr), eth_proto_to_str(proto));
if (gre0_hdr_get_checksum_flag(hdr)) if (gre0_hdr_get_checksum_flag(hdr))
{ {
used += snprintf(buf + used, size - used, ", checksum=%u, offset=%u", gre0_hdr_get_checksum(hdr), gre0_hdr_get_offset(hdr)); used += snprintf(buf + used, size - used, ", checksum=0x%x, offset=%u", gre0_hdr_get_checksum(hdr), gre0_hdr_get_offset(hdr));
} }
if (gre0_hdr_get_key_flag(hdr)) if (gre0_hdr_get_key_flag(hdr))
{ {

View File

@@ -7,14 +7,15 @@
#include "ip4_utils.h" #include "ip4_utils.h"
#include "ip6_utils.h" #include "ip6_utils.h"
#include "gtp_utils.h" #include "gtp_utils.h"
#include "gre_utils.h"
#include "packet_def.h" #include "packet_def.h"
#include "packet_utils.h" #include "packet_utils.h"
#include "packet_layer.h" #include "packet_layer.h"
#include "packet_parse.h" #include "packet_parse.h"
#include "packet_build.h" #include "packet_craft.h"
#define PACKET_BUILD_LOG_DEBUG(format, ...) LOG_DEBUG("packet build", format, ##__VA_ARGS__) #define PACKET_CRAFT_LOG_DEBUG(format, ...) LOG_DEBUG("packet craft", format, ##__VA_ARGS__)
#define PACKET_BUILD_LOG_ERROR(format, ...) LOG_ERROR("packet build", format, ##__VA_ARGS__) #define PACKET_CRAFT_LOG_ERROR(format, ...) LOG_ERROR("packet craft", format, ##__VA_ARGS__)
struct fingerprint struct fingerprint
{ {
@@ -24,15 +25,30 @@ struct fingerprint
uint16_t tcp_win; uint16_t tcp_win;
}; };
static uint8_t append_sender_fingerprint = 0;
/******************************************************************************
* Private API
******************************************************************************/
static inline void calc_packet_fingerprint(struct fingerprint *finger) static inline void calc_packet_fingerprint(struct fingerprint *finger)
{ {
if (append_sender_fingerprint)
{
#define RANGE(rand, start, end) (start + rand % (end - start + 1)) // [start, end] #define RANGE(rand, start, end) (start + rand % (end - start + 1)) // [start, end]
struct timespec time; struct timespec time;
clock_gettime(CLOCK_MONOTONIC, &time); clock_gettime(CLOCK_MONOTONIC, &time);
uint64_t random = 0x013579ABCDEF ^ time.tv_nsec; uint64_t random = 0x013579ABCDEF ^ time.tv_nsec;
finger->ip_id = (uint16_t)(RANGE(random, 32767, 65535)); finger->ip_id = (uint16_t)(RANGE(random, 32767, 65535));
finger->ip_ttl = (uint8_t)(RANGE(random, 48, 120)); finger->ip_ttl = (uint8_t)(RANGE(random, 48, 120));
finger->tcp_win = (uint16_t)(RANGE(random, 1000, 1460)); finger->tcp_win = (uint16_t)(RANGE(random, 1000, 1460));
}
else
{
finger->ip_id = 0;
finger->ip_ttl = 0;
finger->tcp_win = 0;
}
} }
static void update_tcp_hdr(struct tcphdr *tcp, uint32_t seq, uint32_t ack, uint16_t win, uint8_t flags, uint16_t opts_len) static void update_tcp_hdr(struct tcphdr *tcp, uint32_t seq, uint32_t ack, uint16_t win, uint8_t flags, uint16_t opts_len)
@@ -41,7 +57,10 @@ static void update_tcp_hdr(struct tcphdr *tcp, uint32_t seq, uint32_t ack, uint1
tcp_hdr_set_ack(tcp, ack); tcp_hdr_set_ack(tcp, ack);
tcp_hdr_set_hdr_len(tcp, sizeof(struct tcphdr) + opts_len); tcp_hdr_set_hdr_len(tcp, sizeof(struct tcphdr) + opts_len);
tcp_hdr_set_flags(tcp, flags); tcp_hdr_set_flags(tcp, flags);
tcp_hdr_set_window(tcp, win); if (win)
{
tcp_hdr_set_window(tcp, win);
}
tcp_hdr_set_urg_ptr(tcp, 0); tcp_hdr_set_urg_ptr(tcp, 0);
tcp_hdr_set_checksum(tcp, 0); tcp_hdr_set_checksum(tcp, 0);
} }
@@ -58,10 +77,16 @@ static void update_ip4_hdr(struct ip *ip, uint16_t ipid, uint8_t ttl, int trim_l
int hdr_len = ip4_hdr_get_hdr_len(ip); int hdr_len = ip4_hdr_get_hdr_len(ip);
uint16_t total = ip4_hdr_get_total_len(ip); uint16_t total = ip4_hdr_get_total_len(ip);
ip4_hdr_set_total_len(ip, total - trim_len); ip4_hdr_set_total_len(ip, total - trim_len);
ip4_hdr_set_ipid(ip, ipid); if (ipid)
ip4_hdr_set_ttl(ip, ttl); {
ip4_hdr_set_ipid(ip, ipid);
}
if (ttl)
{
ip4_hdr_set_ttl(ip, ttl);
}
ip->ip_sum = 0; ip->ip_sum = 0;
ip->ip_sum = checksum((const char *)ip, hdr_len); ip->ip_sum = checksum((const void *)ip, hdr_len);
} }
static void update_ip6_hdr(struct ip6_hdr *ip6, int trim_len) static void update_ip6_hdr(struct ip6_hdr *ip6, int trim_len)
@@ -76,7 +101,7 @@ static void update_gtp1_hdr(struct gtp1_hdr *gtp, int trim_len)
gtp1_hdr_set_msg_len(gtp, msg_len - trim_len); gtp1_hdr_set_msg_len(gtp, msg_len - trim_len);
if (gtp1_hdr_get_seq_flag(gtp) && gtp1_hdr_get_seq(gtp)) if (gtp1_hdr_get_seq_flag(gtp) && gtp1_hdr_get_seq(gtp))
{ {
PACKET_BUILD_LOG_ERROR("imiated packets may be dropped by intermediate devices, the GTPv1 layer requires a sequence number"); PACKET_CRAFT_LOG_ERROR("crafted packets may be dropped by intermediate devices, the GTPv1 layer requires a sequence number");
} }
} }
@@ -86,22 +111,32 @@ static void update_gtp2_hdr(struct gtp2_hdr *gtp, int trim_len)
gtp2_hdr_set_msg_len(gtp, msg_len - trim_len); gtp2_hdr_set_msg_len(gtp, msg_len - trim_len);
if (gtp2_hdr_get_seq(gtp)) if (gtp2_hdr_get_seq(gtp))
{ {
PACKET_BUILD_LOG_ERROR("imiated packets may be dropped by intermediate devices, the GTPv2 layer requires a sequence number"); PACKET_CRAFT_LOG_ERROR("crafted packets may be dropped by intermediate devices, the GTPv2 layer requires a sequence number");
} }
} }
static void update_gre1_hdr(struct gre1_hdr *gre, int trim_len)
{
uint16_t payload_len = gre1_hdr_get_payload_length(gre);
gre1_hdr_set_payload_length(gre, payload_len - trim_len);
}
static void update_packet_hdr(const struct packet *origin_pkt, static void update_packet_hdr(const struct packet *origin_pkt,
char *new_pkt_data, uint16_t new_pkt_len, int trim_len, char *new_pkt_data, uint16_t new_pkt_len, int trim_len,
uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, uint16_t tcp_opts_len) uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, uint16_t tcp_opts_len)
{ {
uint8_t gtp_version = 0; uint8_t version = 0;
uint16_t sum = 0;
char *curr_hdr_ptr = NULL; char *curr_hdr_ptr = NULL;
char *last_hdr_ptr = NULL;
struct tcphdr *tcp = NULL; struct tcphdr *tcp = NULL;
struct udphdr *udp = NULL; struct udphdr *udp = NULL;
struct ip *ip4 = NULL; struct ip *ip4 = NULL;
struct ip6_hdr *ip6 = NULL; struct ip6_hdr *ip6 = NULL;
struct gtp1_hdr *gtp1 = NULL; struct gtp1_hdr *gtp1 = NULL;
struct gtp2_hdr *gtp2 = NULL; struct gtp2_hdr *gtp2 = NULL;
struct gre0_hdr *gre0 = NULL;
struct gre1_hdr *gre1 = NULL;
struct raw_layer *curr_layer = NULL; struct raw_layer *curr_layer = NULL;
struct raw_layer *last_layer = NULL; struct raw_layer *last_layer = NULL;
struct fingerprint finger = {0}; struct fingerprint finger = {0};
@@ -110,7 +145,9 @@ static void update_packet_hdr(const struct packet *origin_pkt,
for (int i = count - 1; i >= 0; i--) for (int i = count - 1; i >= 0; i--)
{ {
curr_layer = (struct raw_layer *)packet_get_raw_layer(origin_pkt, i); curr_layer = (struct raw_layer *)packet_get_raw_layer(origin_pkt, i);
last_layer = (struct raw_layer *)packet_get_raw_layer(origin_pkt, i + 1);
curr_hdr_ptr = new_pkt_data + curr_layer->hdr_offset; curr_hdr_ptr = new_pkt_data + curr_layer->hdr_offset;
last_hdr_ptr = last_layer ? new_pkt_data + last_layer->hdr_offset : NULL;
switch (curr_layer->proto) switch (curr_layer->proto)
{ {
case LAYER_PROTO_TCP: case LAYER_PROTO_TCP:
@@ -123,50 +160,63 @@ static void update_packet_hdr(const struct packet *origin_pkt,
break; break;
case LAYER_PROTO_IPV4: case LAYER_PROTO_IPV4:
ip4 = (struct ip *)curr_hdr_ptr; ip4 = (struct ip *)curr_hdr_ptr;
last_layer = (struct raw_layer *)packet_get_raw_layer(origin_pkt, i + 1); if (last_layer && last_layer->proto == LAYER_PROTO_TCP)
if (last_layer->proto == LAYER_PROTO_TCP)
{ {
tcp = (struct tcphdr *)(new_pkt_data + last_layer->hdr_offset); tcp = (struct tcphdr *)last_hdr_ptr;
tcp->th_sum = checksum_v4(tcp, new_pkt_len - last_layer->hdr_offset, IPPROTO_TCP, &ip4->ip_src, &ip4->ip_dst); tcp->th_sum = checksum_v4(tcp, new_pkt_len - last_layer->hdr_offset, IPPROTO_TCP, &ip4->ip_src, &ip4->ip_dst);
} }
if (last_layer->proto == LAYER_PROTO_UDP) if (last_layer && last_layer->proto == LAYER_PROTO_UDP)
{ {
udp = (struct udphdr *)(new_pkt_data + last_layer->hdr_offset); udp = (struct udphdr *)last_hdr_ptr;
udp->uh_sum = checksum_v4(udp, new_pkt_len - last_layer->hdr_offset, IPPROTO_UDP, &ip4->ip_src, &ip4->ip_dst); udp->uh_sum = checksum_v4(udp, new_pkt_len - last_layer->hdr_offset, IPPROTO_UDP, &ip4->ip_src, &ip4->ip_dst);
} }
update_ip4_hdr(ip4, finger.ip_id, finger.ip_ttl, trim_len); update_ip4_hdr(ip4, finger.ip_id, finger.ip_ttl, trim_len);
break; break;
case LAYER_PROTO_IPV6: case LAYER_PROTO_IPV6:
ip6 = (struct ip6_hdr *)curr_hdr_ptr; ip6 = (struct ip6_hdr *)curr_hdr_ptr;
last_layer = (struct raw_layer *)packet_get_raw_layer(origin_pkt, i + 1); if (last_layer && last_layer->proto == LAYER_PROTO_TCP)
if (last_layer->proto == LAYER_PROTO_TCP)
{ {
tcp = (struct tcphdr *)(new_pkt_data + last_layer->hdr_offset); tcp = (struct tcphdr *)last_hdr_ptr;
tcp->th_sum = checksum_v6(tcp, new_pkt_len - last_layer->hdr_offset, IPPROTO_TCP, &ip6->ip6_src, &ip6->ip6_dst); tcp->th_sum = checksum_v6(tcp, new_pkt_len - last_layer->hdr_offset, IPPROTO_TCP, &ip6->ip6_src, &ip6->ip6_dst);
} }
if (last_layer->proto == LAYER_PROTO_UDP) if (last_layer && last_layer->proto == LAYER_PROTO_UDP)
{ {
udp = (struct udphdr *)(new_pkt_data + last_layer->hdr_offset); udp = (struct udphdr *)last_hdr_ptr;
udp->uh_sum = checksum_v6(udp, new_pkt_len - last_layer->hdr_offset, IPPROTO_UDP, &ip6->ip6_src, &ip6->ip6_dst); udp->uh_sum = checksum_v6(udp, new_pkt_len - last_layer->hdr_offset, IPPROTO_UDP, &ip6->ip6_src, &ip6->ip6_dst);
} }
update_ip6_hdr(ip6, trim_len); update_ip6_hdr(ip6, trim_len);
break; break;
case LAYER_PROTO_GTP_C: /* fall through */ case LAYER_PROTO_GTP_C: /* fall through */
case LAYER_PROTO_GTP_U: case LAYER_PROTO_GTP_U:
gtp_version = peek_gtp_version(new_pkt_data + curr_layer->hdr_offset, curr_layer->hdr_len); version = peek_gtp_version(curr_hdr_ptr, curr_layer->hdr_len);
if (gtp_version == 1) if (version == 1)
{ {
gtp1 = (struct gtp1_hdr *)curr_hdr_ptr; gtp1 = (struct gtp1_hdr *)curr_hdr_ptr;
update_gtp1_hdr(gtp1, trim_len); update_gtp1_hdr(gtp1, trim_len);
} }
if (gtp_version == 2) if (version == 2)
{ {
gtp2 = (struct gtp2_hdr *)curr_hdr_ptr; gtp2 = (struct gtp2_hdr *)curr_hdr_ptr;
update_gtp2_hdr(gtp2, trim_len); update_gtp2_hdr(gtp2, trim_len);
} }
break; break;
case LAYER_PROTO_GRE: case LAYER_PROTO_GRE:
// TODO version = peek_gre_version(curr_hdr_ptr, curr_layer->hdr_len);
if (version == 0)
{
gre0 = (struct gre0_hdr *)curr_hdr_ptr;
if (gre0_hdr_get_checksum_flag(gre0))
{
gre0_hdr_set_checksum(gre0, ntohs(0));
sum = checksum((const void *)curr_hdr_ptr, new_pkt_len - curr_layer->hdr_offset);
gre0_hdr_set_checksum(gre0, ntohs(sum));
}
}
if (version == 1)
{
gre1 = (struct gre1_hdr *)curr_hdr_ptr;
update_gre1_hdr(gre1, trim_len);
}
break; break;
default: default:
break; break;
@@ -174,14 +224,23 @@ static void update_packet_hdr(const struct packet *origin_pkt,
} }
} }
/******************************************************************************
* Public API
******************************************************************************/
void append_sender_fingerprint_to_crafted_packet()
{
append_sender_fingerprint = 1;
}
/* /*
* tcp_seq: the sequence number of the new TCP packet (in host byte order) * tcp_seq: the sequence number of the new TCP packet (in host byte order)
* tcp_ack: the acknowledgment number of the new TCP packet (in host byte order) * tcp_ack: the acknowledgment number of the new TCP packet (in host byte order)
* tcp_options_len: the length of the options (must be a multiple of 4) * tcp_options_len: the length of the options (must be a multiple of 4)
*/ */
struct packet *imitate_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, struct packet *craft_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags,
const char *tcp_options, uint16_t tcp_options_len, const char *tcp_options, uint16_t tcp_options_len,
const char *tcp_payload, uint16_t tcp_payload_len) const char *tcp_payload, uint16_t tcp_payload_len)
{ {
// check arguments // check arguments
if (origin_pkt == NULL || if (origin_pkt == NULL ||
@@ -189,7 +248,7 @@ struct packet *imitate_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_
(tcp_payload == NULL && tcp_payload_len != 0) || (tcp_payload != NULL && tcp_payload_len == 0) || (tcp_payload == NULL && tcp_payload_len != 0) || (tcp_payload != NULL && tcp_payload_len == 0) ||
(tcp_options_len && tcp_options_len % 4 != 0)) (tcp_options_len && tcp_options_len % 4 != 0))
{ {
PACKET_BUILD_LOG_ERROR("imitate TCP packet failed, invalid arguments"); PACKET_CRAFT_LOG_ERROR("craft TCP packet failed, invalid arguments");
return NULL; return NULL;
} }
@@ -198,7 +257,7 @@ struct packet *imitate_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_
const struct raw_layer *tcp_layer = packet_get_raw_layer(origin_pkt, layers - 1); const struct raw_layer *tcp_layer = packet_get_raw_layer(origin_pkt, layers - 1);
if (tcp_layer == NULL || tcp_layer->proto != LAYER_PROTO_TCP) if (tcp_layer == NULL || tcp_layer->proto != LAYER_PROTO_TCP)
{ {
PACKET_BUILD_LOG_ERROR("imitate TCP packet failed, the innermost layer of the original packet is not TCP"); PACKET_CRAFT_LOG_ERROR("craft TCP packet failed, the innermost layer of the original packet is not TCP");
return NULL; return NULL;
} }
@@ -208,7 +267,7 @@ struct packet *imitate_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_
struct packet *new_pkt = packet_new(new_pkt_len); struct packet *new_pkt = packet_new(new_pkt_len);
if (new_pkt == NULL) if (new_pkt == NULL)
{ {
PACKET_BUILD_LOG_ERROR("imitate TCP packet failed, no space to allocate new packet"); PACKET_CRAFT_LOG_ERROR("craft TCP packet failed, no space to allocate new packet");
return NULL; return NULL;
} }
@@ -231,12 +290,12 @@ struct packet *imitate_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_
return new_pkt; return new_pkt;
} }
struct packet *imitate_udp_packet(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len) struct packet *craft_udp_packet(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len)
{ {
// check arguments // check arguments
if (origin_pkt == NULL || (udp_payload == NULL && udp_payload_len != 0) || (udp_payload != NULL && udp_payload_len == 0)) if (origin_pkt == NULL || (udp_payload == NULL && udp_payload_len != 0) || (udp_payload != NULL && udp_payload_len == 0))
{ {
PACKET_BUILD_LOG_ERROR("imitate UDP packet failed, invalid arguments"); PACKET_CRAFT_LOG_ERROR("craft UDP packet failed, invalid arguments");
return NULL; return NULL;
} }
@@ -245,7 +304,7 @@ struct packet *imitate_udp_packet(const struct packet *origin_pkt, const char *u
const struct raw_layer *udp_layer = packet_get_raw_layer(origin_pkt, layers - 1); const struct raw_layer *udp_layer = packet_get_raw_layer(origin_pkt, layers - 1);
if (udp_layer == NULL || udp_layer->proto != LAYER_PROTO_UDP) if (udp_layer == NULL || udp_layer->proto != LAYER_PROTO_UDP)
{ {
PACKET_BUILD_LOG_ERROR("imitate UDP packet failed, the innermost layer of the original packet is not UDP"); PACKET_CRAFT_LOG_ERROR("craft UDP packet failed, the innermost layer of the original packet is not UDP");
return NULL; return NULL;
} }
@@ -255,7 +314,7 @@ struct packet *imitate_udp_packet(const struct packet *origin_pkt, const char *u
struct packet *new_pkt = packet_new(new_pkt_len); struct packet *new_pkt = packet_new(new_pkt_len);
if (new_pkt == NULL) if (new_pkt == NULL)
{ {
PACKET_BUILD_LOG_ERROR("imitate UDP packet failed, no space to allocate new packet"); PACKET_CRAFT_LOG_ERROR("craft UDP packet failed, no space to allocate new packet");
return NULL; return NULL;
} }
@@ -279,7 +338,7 @@ struct packet *craft_packet_from_scratch(const struct layer layers[], uint16_t l
// check arguments // check arguments
if (layers == NULL || layer_count == 0 || (payload == NULL && payload_len != 0) || (payload != NULL && payload_len == 0)) if (layers == NULL || layer_count == 0 || (payload == NULL && payload_len != 0) || (payload != NULL && payload_len == 0))
{ {
PACKET_BUILD_LOG_ERROR("craft packet from scratch failed, invalid arguments"); PACKET_CRAFT_LOG_ERROR("craft packet from scratch failed, invalid arguments");
return NULL; return NULL;
} }
@@ -289,7 +348,7 @@ struct packet *craft_packet_from_scratch(const struct layer layers[], uint16_t l
{ {
if (layers[i].hdr.raw == NULL || layers[i].hdr_len == 0) if (layers[i].hdr.raw == NULL || layers[i].hdr_len == 0)
{ {
PACKET_BUILD_LOG_ERROR("craft packet from scratch failed, the header of layer %d is invalid", i); PACKET_CRAFT_LOG_ERROR("craft packet from scratch failed, the header of layer %d is invalid", i);
return NULL; return NULL;
} }
new_pkt_len += layers[i].hdr_len; new_pkt_len += layers[i].hdr_len;
@@ -298,7 +357,7 @@ struct packet *craft_packet_from_scratch(const struct layer layers[], uint16_t l
struct packet *new_pkt = packet_new(new_pkt_len); struct packet *new_pkt = packet_new(new_pkt_len);
if (new_pkt == NULL) if (new_pkt == NULL)
{ {
PACKET_BUILD_LOG_ERROR("craft packet from scratch failed, no space to allocate new packet"); PACKET_CRAFT_LOG_ERROR("craft packet from scratch failed, no space to allocate new packet");
return NULL; return NULL;
} }
@@ -313,7 +372,7 @@ struct packet *craft_packet_from_scratch(const struct layer layers[], uint16_t l
memcpy(new_pkt_data + offset, payload, payload_len); memcpy(new_pkt_data + offset, payload, payload_len);
// update the headers of the new packet // update the headers of the new packet
uint8_t gtp_version = 0; uint8_t version = 0;
uint16_t curr_hdr_len = 0; uint16_t curr_hdr_len = 0;
char *curr_hdr_ptr = NULL; char *curr_hdr_ptr = NULL;
struct tcphdr *tcp = NULL; struct tcphdr *tcp = NULL;
@@ -361,7 +420,7 @@ struct packet *craft_packet_from_scratch(const struct layer layers[], uint16_t l
ip4_hdr_set_hdr_len(ip4, curr_hdr_len); ip4_hdr_set_hdr_len(ip4, curr_hdr_len);
ip4_hdr_set_total_len(ip4, curr_hdr_len + curr_payload_len); ip4_hdr_set_total_len(ip4, curr_hdr_len + curr_payload_len);
ip4->ip_sum = 0; ip4->ip_sum = 0;
ip4->ip_sum = checksum((const char *)ip4, curr_hdr_len); ip4->ip_sum = checksum((const void *)ip4, curr_hdr_len);
curr_payload_len += curr_hdr_len; curr_payload_len += curr_hdr_len;
break; break;
case LAYER_PROTO_IPV6: case LAYER_PROTO_IPV6:
@@ -383,14 +442,14 @@ struct packet *craft_packet_from_scratch(const struct layer layers[], uint16_t l
break; break;
case LAYER_PROTO_GTP_C: /* fall through */ case LAYER_PROTO_GTP_C: /* fall through */
case LAYER_PROTO_GTP_U: case LAYER_PROTO_GTP_U:
gtp_version = peek_gtp_version(curr_hdr_ptr, curr_hdr_len); version = peek_gtp_version(curr_hdr_ptr, curr_hdr_len);
if (gtp_version == 1) if (version == 1)
{ {
gtp1 = (struct gtp1_hdr *)curr_hdr_ptr; gtp1 = (struct gtp1_hdr *)curr_hdr_ptr;
// update the GTP header // update the GTP header
gtp1_hdr_set_msg_len(gtp1, curr_hdr_len + curr_payload_len - sizeof(struct gtp1_hdr)); gtp1_hdr_set_msg_len(gtp1, curr_hdr_len + curr_payload_len - sizeof(struct gtp1_hdr));
} }
if (gtp_version == 2) if (version == 2)
{ {
gtp2 = (struct gtp2_hdr *)curr_hdr_ptr; gtp2 = (struct gtp2_hdr *)curr_hdr_ptr;
// update the GTP header // update the GTP header

View File

@@ -7,15 +7,17 @@ extern "C"
#include <stdint.h> #include <stdint.h>
void append_sender_fingerprint_to_crafted_packet();
/* /*
* tcp_seq: the sequence number of the new TCP packet (in host byte order) * tcp_seq: the sequence number of the new TCP packet (in host byte order)
* tcp_ack: the acknowledgment number of the new TCP packet (in host byte order) * tcp_ack: the acknowledgment number of the new TCP packet (in host byte order)
* tcp_options_len: the length of the options (must be a multiple of 4) * tcp_options_len: the length of the options (must be a multiple of 4)
*/ */
struct packet *imitate_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, struct packet *craft_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags,
const char *tcp_options, uint16_t tcp_options_len, const char *tcp_options, uint16_t tcp_options_len,
const char *tcp_payload, uint16_t tcp_payload_len); const char *tcp_payload, uint16_t tcp_payload_len);
struct packet *imitate_udp_packet(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len); struct packet *craft_udp_packet(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len);
struct packet *craft_packet_from_scratch(const struct layer larers[], uint16_t layer_count, const char *payload, uint16_t payload_len); struct packet *craft_packet_from_scratch(const struct layer larers[], uint16_t layer_count, const char *payload, uint16_t payload_len);
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -157,7 +157,7 @@ static inline struct raw_layer *get_free_layer(struct packet *pkt)
(_layer)->pld_len = (_len) - (_hdr_len) - (_trim); \ (_layer)->pld_len = (_len) - (_hdr_len) - (_trim); \
(_pkt)->trim_len += (_trim); \ (_pkt)->trim_len += (_trim); \
(_pkt)->layers_used++; \ (_pkt)->layers_used++; \
PACKET_PARSE_LOG_DEBUG("layer[%d/%d]: %s, hdr_offset: %d, hdr_ptr: %p, hdr_len: %d, pld_ptr: %p, pld_len: %d", \ PACKET_PARSE_LOG_DEBUG("layer[%d/%d]: %s, header: {offset: %d, ptr: %p, len: %d}, payload: {ptr: %p, len: %d}", \
(_pkt)->layers_used - 1, (_pkt)->layers_size, layer_proto_to_str((_proto)), \ (_pkt)->layers_used - 1, (_pkt)->layers_size, layer_proto_to_str((_proto)), \
(_layer)->hdr_offset, (_layer)->hdr_ptr, (_layer)->hdr_len, (_layer)->pld_ptr, (_layer)->pld_len); \ (_layer)->hdr_offset, (_layer)->hdr_ptr, (_layer)->hdr_len, (_layer)->pld_ptr, (_layer)->pld_len); \
} }
@@ -1001,7 +1001,7 @@ void packet_print(const struct packet *pkt)
{ {
int used = 0; int used = 0;
const struct raw_layer *layer = &pkt->layers[i]; const struct raw_layer *layer = &pkt->layers[i];
printf(" layer[%u]: %p, proto: %s, hdr_offset: %u, hdr_ptr: %p, hdr_len: %u, pld_ptr: %p, pld_len: %u\n", printf(" layer[%u]: %p, proto: %s, header: {offset: %u, ptr: %p, len: %u}, payload: {ptr: %p, len: %u}\n",
i, layer, layer_proto_to_str(layer->proto), layer->hdr_offset, i, layer, layer_proto_to_str(layer->proto), layer->hdr_offset,
layer->hdr_ptr, layer->hdr_len, layer->pld_ptr, layer->pld_len); layer->hdr_ptr, layer->hdr_len, layer->pld_ptr, layer->pld_len);
switch (layer->proto) switch (layer->proto)

View File

@@ -46,8 +46,8 @@ target_link_libraries(gtest_packet_frag packet gtest)
add_executable(gtest_packet_parse gtest_packet_parse.cpp) add_executable(gtest_packet_parse gtest_packet_parse.cpp)
target_link_libraries(gtest_packet_parse packet gtest) target_link_libraries(gtest_packet_parse packet gtest)
add_executable(gtest_packet_build gtest_packet_build.cpp) add_executable(gtest_packet_craft gtest_packet_craft.cpp)
target_link_libraries(gtest_packet_build packet gtest) target_link_libraries(gtest_packet_craft packet gtest)
add_executable(gtest_packet_ldbc gtest_packet_ldbc.cpp) add_executable(gtest_packet_ldbc gtest_packet_ldbc.cpp)
target_link_libraries(gtest_packet_ldbc packet gtest) target_link_libraries(gtest_packet_ldbc packet gtest)
@@ -69,5 +69,5 @@ gtest_discover_tests(gtest_gtp1_utils)
gtest_discover_tests(gtest_gtp2_utils) gtest_discover_tests(gtest_gtp2_utils)
gtest_discover_tests(gtest_packet_frag) gtest_discover_tests(gtest_packet_frag)
gtest_discover_tests(gtest_packet_parse) gtest_discover_tests(gtest_packet_parse)
gtest_discover_tests(gtest_packet_build) gtest_discover_tests(gtest_packet_craft)
gtest_discover_tests(gtest_packet_ldbc) gtest_discover_tests(gtest_packet_ldbc)

View File

@@ -6,11 +6,12 @@
#include "ip4_utils.h" #include "ip4_utils.h"
#include "ip6_utils.h" #include "ip6_utils.h"
#include "gtp_utils.h" #include "gtp_utils.h"
#include "gre_utils.h"
#include "packet_def.h" #include "packet_def.h"
#include "packet_dump.h" #include "packet_dump.h"
#include "packet_layer.h" #include "packet_layer.h"
#include "packet_parse.h" #include "packet_parse.h"
#include "packet_build.h" #include "packet_craft.h"
#define PRINT_GREEN(fmt, ...) printf("\033[0;32m" fmt "\033[0m\n", ##__VA_ARGS__) #define PRINT_GREEN(fmt, ...) printf("\033[0;32m" fmt "\033[0m\n", ##__VA_ARGS__)
#define PRINT_RED(fmt, ...) printf("\033[0;31m" fmt "\033[0m\n", ##__VA_ARGS__) #define PRINT_RED(fmt, ...) printf("\033[0;31m" fmt "\033[0m\n", ##__VA_ARGS__)
@@ -120,13 +121,13 @@ unsigned char data1[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
/* /*
* imitate_tcp_packet() * craft_tcp_packet()
* -> ETH->IPv4->TCP * -> ETH->IPv4->TCP
* -> with TCP options * -> with TCP options
* -> with TCP payload * -> with TCP payload
*/ */
#if 1 #if 1
TEST(PACKET_BUILD_TCP, ETH_IP4_TCP) TEST(PACKET_CRAFT_TCP, ETH_IP4_TCP)
{ {
struct packet orig_pkt; struct packet orig_pkt;
memset(&orig_pkt, 0, sizeof(orig_pkt)); memset(&orig_pkt, 0, sizeof(orig_pkt));
@@ -134,13 +135,13 @@ TEST(PACKET_BUILD_TCP, ETH_IP4_TCP)
PRINT_GREEN("origin packet:"); PRINT_GREEN("origin packet:");
packet_print(&orig_pkt); packet_print(&orig_pkt);
struct packet *new_pkt = imitate_tcp_packet(&orig_pkt, 1, 2, TH_ACK, (const char *)&ts_pad_opt, sizeof(ts_pad_opt), "Hello", 5); struct packet *new_pkt = craft_tcp_packet(&orig_pkt, 1, 2, TH_ACK, (const char *)&ts_pad_opt, sizeof(ts_pad_opt), "Hello", 5);
EXPECT_TRUE(new_pkt != nullptr); EXPECT_TRUE(new_pkt != nullptr);
PRINT_GREEN("new packet:"); PRINT_GREEN("new packet:");
packet_print(new_pkt); packet_print(new_pkt);
packet_dump_hex(new_pkt, STDOUT_FILENO); packet_dump_hex(new_pkt, STDOUT_FILENO);
packet_dump_pcap(new_pkt, "imitate-eth-ipv4-tcp.pcap"); packet_dump_pcap(new_pkt, "craft-eth-ipv4-tcp.pcap");
const char *orig_pkt_data = packet_get_raw_data(&orig_pkt); const char *orig_pkt_data = packet_get_raw_data(&orig_pkt);
uint16_t orig_pkt_len = packet_get_raw_len(&orig_pkt); uint16_t orig_pkt_len = packet_get_raw_len(&orig_pkt);
@@ -155,25 +156,25 @@ TEST(PACKET_BUILD_TCP, ETH_IP4_TCP)
{ {
if (layer.proto == LAYER_PROTO_IPV4) if (layer.proto == LAYER_PROTO_IPV4)
{ {
const struct ip *ip = (struct ip *)layer.hdr.raw; const struct ip *ip = (const struct ip *)layer.hdr.raw;
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 57); EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 57);
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0xb7e1);
break; break;
} }
if (layer.proto == LAYER_PROTO_TCP) if (layer.proto == LAYER_PROTO_TCP)
{ {
const struct tcphdr *tcp = (struct tcphdr *)layer.hdr.raw; const struct tcphdr *tcp = (const struct tcphdr *)layer.hdr.raw;
EXPECT_TRUE(tcp_hdr_get_seq(tcp) == 1); EXPECT_TRUE(tcp_hdr_get_seq(tcp) == 1);
EXPECT_TRUE(tcp_hdr_get_ack(tcp) == 2); EXPECT_TRUE(tcp_hdr_get_ack(tcp) == 2);
EXPECT_TRUE(tcp_hdr_get_flags(tcp) == TH_ACK); EXPECT_TRUE(tcp_hdr_get_flags(tcp) == TH_ACK);
EXPECT_TRUE(tcp_hdr_get_hdr_len(tcp) == 32); EXPECT_TRUE(tcp_hdr_get_hdr_len(tcp) == 32);
EXPECT_TRUE(tcp_hdr_get_checksum(tcp) == 0xaf73);
break; break;
} }
} }
for (uint16_t i = 0; i < new_pkt_len - 12 - 5; i++) for (uint16_t i = 0; i < new_pkt_len - 12 - 5; i++)
{ {
if ((16 <= i && i <= 17) || // skip IPv4 total length if ((16 <= i && i <= 17) || // skip IPv4 total length
(18 <= i && i <= 19) || // skip IPv4 identification
i == 22 || // skip IPv4 TTL
(24 <= i && i <= 25)) // skip IPv4 checksum (24 <= i && i <= 25)) // skip IPv4 checksum
{ {
continue; continue;
@@ -182,7 +183,6 @@ TEST(PACKET_BUILD_TCP, ETH_IP4_TCP)
(42 <= i && i <= 45) || // skip TCP ack (42 <= i && i <= 45) || // skip TCP ack
i == 46 || // skip TCP data offset i == 46 || // skip TCP data offset
i == 47 || // skip TCP flags i == 47 || // skip TCP flags
(48 <= i && i <= 49) || // skip TCP window
(50 <= i && i <= 51)) // skip TCP checksum (50 <= i && i <= 51)) // skip TCP checksum
{ {
continue; continue;
@@ -257,12 +257,12 @@ unsigned char data2[] = {
0x04, 0x02}; 0x04, 0x02};
/* /*
* imitate_tcp_packet() * craft_tcp_packet()
* -> ETH->IPv4->IPv6->TCP * -> ETH->IPv4->IPv6->TCP
* -> with TCP payload * -> with TCP payload
*/ */
#if 1 #if 1
TEST(PACKET_BUILD_TCP, ETH_IP4_IP6_TCP) TEST(PACKET_CRAFT_TCP, ETH_IP4_IP6_TCP)
{ {
struct packet orig_pkt; struct packet orig_pkt;
memset(&orig_pkt, 0, sizeof(orig_pkt)); memset(&orig_pkt, 0, sizeof(orig_pkt));
@@ -270,13 +270,13 @@ TEST(PACKET_BUILD_TCP, ETH_IP4_IP6_TCP)
PRINT_GREEN("origin packet:"); PRINT_GREEN("origin packet:");
packet_print(&orig_pkt); packet_print(&orig_pkt);
struct packet *new_pkt = imitate_tcp_packet(&orig_pkt, 1234, 2345, TH_ACK, NULL, 0, "Hello", 5); struct packet *new_pkt = craft_tcp_packet(&orig_pkt, 1234, 2345, TH_ACK, NULL, 0, "Hello", 5);
EXPECT_TRUE(new_pkt != nullptr); EXPECT_TRUE(new_pkt != nullptr);
PRINT_GREEN("new packet:"); PRINT_GREEN("new packet:");
packet_print(new_pkt); packet_print(new_pkt);
packet_dump_hex(new_pkt, STDOUT_FILENO); packet_dump_hex(new_pkt, STDOUT_FILENO);
packet_dump_pcap(new_pkt, "imitate-eth-ipv4-ipv6-tcp.pcap"); packet_dump_pcap(new_pkt, "craft-eth-ipv4-ipv6-tcp.pcap");
const char *orig_pkt_data = packet_get_raw_data(&orig_pkt); const char *orig_pkt_data = packet_get_raw_data(&orig_pkt);
uint16_t orig_pkt_len = packet_get_raw_len(&orig_pkt); uint16_t orig_pkt_len = packet_get_raw_len(&orig_pkt);
@@ -291,37 +291,36 @@ TEST(PACKET_BUILD_TCP, ETH_IP4_IP6_TCP)
{ {
if (layer.proto == LAYER_PROTO_IPV4) if (layer.proto == LAYER_PROTO_IPV4)
{ {
const struct ip *ip = (struct ip *)layer.hdr.raw; const struct ip *ip = (const struct ip *)layer.hdr.raw;
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 85); EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 85);
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x09cf);
break; break;
} }
if (layer.proto == LAYER_PROTO_IPV6) if (layer.proto == LAYER_PROTO_IPV6)
{ {
const struct ip6_hdr *ip6 = (struct ip6_hdr *)layer.hdr.raw; const struct ip6_hdr *ip6 = (const struct ip6_hdr *)layer.hdr.raw;
EXPECT_TRUE(ip6_hdr_get_payload_len(ip6) == 25); EXPECT_TRUE(ip6_hdr_get_payload_len(ip6) == 25);
break; break;
} }
if (layer.proto == LAYER_PROTO_TCP) if (layer.proto == LAYER_PROTO_TCP)
{ {
const struct tcphdr *tcp = (struct tcphdr *)layer.hdr.raw; const struct tcphdr *tcp = (const struct tcphdr *)layer.hdr.raw;
EXPECT_TRUE(tcp_hdr_get_seq(tcp) == 1234); EXPECT_TRUE(tcp_hdr_get_seq(tcp) == 1234);
EXPECT_TRUE(tcp_hdr_get_ack(tcp) == 2345); EXPECT_TRUE(tcp_hdr_get_ack(tcp) == 2345);
EXPECT_TRUE(tcp_hdr_get_flags(tcp) == TH_ACK); EXPECT_TRUE(tcp_hdr_get_flags(tcp) == TH_ACK);
EXPECT_TRUE(tcp_hdr_get_hdr_len(tcp) == 20); EXPECT_TRUE(tcp_hdr_get_hdr_len(tcp) == 20);
EXPECT_TRUE(tcp_hdr_get_checksum(tcp) == 0xe350);
break; break;
} }
} }
for (uint16_t i = 0; i < new_pkt_len - 5; i++) for (uint16_t i = 0; i < new_pkt_len - 5; i++)
{ {
if ((16 <= i && i <= 17) || // skip IPv4 total length if ((16 <= i && i <= 17) || // skip IPv4 total length
(18 <= i && i <= 19) || // skip IPv4 identification
i == 22 || // skip IPv4 TTL
(24 <= i && i <= 25)) // skip IPv4 checksum (24 <= i && i <= 25)) // skip IPv4 checksum
{ {
continue; continue;
} }
if ((38 <= i && i <= 39) || // skip IPv6 payload length if (38 <= i && i <= 39) // skip IPv6 payload length
i == 41) // skip IPv6 hop limit
{ {
continue; continue;
} }
@@ -329,7 +328,6 @@ TEST(PACKET_BUILD_TCP, ETH_IP4_IP6_TCP)
(82 <= i && i <= 85) || // skip TCP ack (82 <= i && i <= 85) || // skip TCP ack
i == 86 || // skip TCP data offset i == 86 || // skip TCP data offset
i == 87 || // skip TCP flags i == 87 || // skip TCP flags
(88 <= i && i <= 89) || // skip TCP window
(90 <= i && i <= 91)) // skip TCP checksum (90 <= i && i <= 91)) // skip TCP checksum
{ {
continue; continue;
@@ -473,12 +471,12 @@ unsigned char data3[] = {
0x4f, 0xe9, 0xf5, 0xf0, 0x61, 0x5d, 0x7f, 0xc4, 0xc4, 0xd1, 0x05, 0x54, 0x13, 0xdb}; 0x4f, 0xe9, 0xf5, 0xf0, 0x61, 0x5d, 0x7f, 0xc4, 0xc4, 0xd1, 0x05, 0x54, 0x13, 0xdb};
/* /*
* imitate_tcp_packet() * craft_tcp_packet()
* -> ETH->IPv6->UDP->GTP->IPv4->TCP * -> ETH->IPv6->UDP->GTP->IPv4->TCP
* -> with TCP payload * -> with TCP payload
*/ */
#if 1 #if 1
TEST(PACKET_BUILD_TCP, ETH_IP6_UDP_GTP_IP4_TCP) TEST(PACKET_CRAFT_TCP, ETH_IP6_UDP_GTP_IP4_TCP)
{ {
struct packet orig_pkt; struct packet orig_pkt;
memset(&orig_pkt, 0, sizeof(orig_pkt)); memset(&orig_pkt, 0, sizeof(orig_pkt));
@@ -486,13 +484,13 @@ TEST(PACKET_BUILD_TCP, ETH_IP6_UDP_GTP_IP4_TCP)
PRINT_GREEN("origin packet:"); PRINT_GREEN("origin packet:");
packet_print(&orig_pkt); packet_print(&orig_pkt);
struct packet *new_pkt = imitate_tcp_packet(&orig_pkt, 1, 2, TH_ACK, NULL, 0, "Hello", 5); struct packet *new_pkt = craft_tcp_packet(&orig_pkt, 1, 2, TH_ACK, NULL, 0, "Hello", 5);
EXPECT_TRUE(new_pkt != nullptr); EXPECT_TRUE(new_pkt != nullptr);
PRINT_GREEN("new packet:"); PRINT_GREEN("new packet:");
packet_print(new_pkt); packet_print(new_pkt);
packet_dump_hex(new_pkt, STDOUT_FILENO); packet_dump_hex(new_pkt, STDOUT_FILENO);
packet_dump_pcap(new_pkt, "imitate-eth-ipv6-udp-gtp-ipv4-tcp.pcap"); packet_dump_pcap(new_pkt, "craft-eth-ipv6-udp-gtp-ipv4-tcp.pcap");
const char *orig_pkt_data = packet_get_raw_data(&orig_pkt); const char *orig_pkt_data = packet_get_raw_data(&orig_pkt);
uint16_t orig_pkt_len = packet_get_raw_len(&orig_pkt); uint16_t orig_pkt_len = packet_get_raw_len(&orig_pkt);
@@ -507,45 +505,46 @@ TEST(PACKET_BUILD_TCP, ETH_IP6_UDP_GTP_IP4_TCP)
{ {
if (layer.proto == LAYER_PROTO_IPV6) if (layer.proto == LAYER_PROTO_IPV6)
{ {
const struct ip6_hdr *ip6 = (struct ip6_hdr *)layer.hdr.raw; const struct ip6_hdr *ip6 = (const struct ip6_hdr *)layer.hdr.raw;
EXPECT_TRUE(ip6_hdr_get_payload_len(ip6) == 61); EXPECT_TRUE(ip6_hdr_get_payload_len(ip6) == 61);
break; break;
} }
if (layer.proto == LAYER_PROTO_UDP) if (layer.proto == LAYER_PROTO_UDP)
{ {
const struct udphdr *udp = (struct udphdr *)layer.hdr.raw; const struct udphdr *udp = (const struct udphdr *)layer.hdr.raw;
EXPECT_TRUE(udp_hdr_get_total_len(udp) == 61); EXPECT_TRUE(udp_hdr_get_total_len(udp) == 61);
EXPECT_TRUE(udp_hdr_get_checksum(udp) == 0xd375);
break; break;
} }
if (layer.proto == LAYER_PROTO_GTP_U) if (layer.proto == LAYER_PROTO_GTP_U)
{ {
EXPECT_TRUE(peek_gtp_version(layer.hdr.raw, layer.hdr_len) == 1); EXPECT_TRUE(peek_gtp_version(layer.hdr.raw, layer.hdr_len) == 1);
const struct gtp1_hdr *gtp1 = (const struct gtp1_hdr *)layer.hdr.raw;
const struct gtp1_hdr *gtp1 = (struct gtp1_hdr *)layer.hdr.raw;
EXPECT_TRUE(gtp1_hdr_get_msg_len(gtp1) == 45); EXPECT_TRUE(gtp1_hdr_get_msg_len(gtp1) == 45);
break; break;
} }
if (layer.proto == LAYER_PROTO_IPV4) if (layer.proto == LAYER_PROTO_IPV4)
{ {
const struct ip *ip = (struct ip *)layer.hdr.raw; const struct ip *ip = (const struct ip *)layer.hdr.raw;
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 45); EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 45);
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x4906);
break; break;
} }
if (layer.proto == LAYER_PROTO_TCP) if (layer.proto == LAYER_PROTO_TCP)
{ {
const struct tcphdr *tcp = (struct tcphdr *)layer.hdr.raw; const struct tcphdr *tcp = (const struct tcphdr *)layer.hdr.raw;
EXPECT_TRUE(tcp_hdr_get_seq(tcp) == 1); EXPECT_TRUE(tcp_hdr_get_seq(tcp) == 1);
EXPECT_TRUE(tcp_hdr_get_ack(tcp) == 2); EXPECT_TRUE(tcp_hdr_get_ack(tcp) == 2);
EXPECT_TRUE(tcp_hdr_get_flags(tcp) == TH_ACK); EXPECT_TRUE(tcp_hdr_get_flags(tcp) == TH_ACK);
EXPECT_TRUE(tcp_hdr_get_hdr_len(tcp) == 20); EXPECT_TRUE(tcp_hdr_get_hdr_len(tcp) == 20);
EXPECT_TRUE(tcp_hdr_get_checksum(tcp) == 0xcce5);
break; break;
} }
} }
for (uint16_t i = 0; i < new_pkt_len - 5; i++) for (uint16_t i = 0; i < new_pkt_len - 5; i++)
{ {
if ((18 <= i && i <= 19) || // skip IPv6 payload length if (18 <= i && i <= 19) // skip IPv6 payload length
i == 20) // skip IPv6 hop limit
{ {
continue; continue;
} }
@@ -559,18 +558,221 @@ TEST(PACKET_BUILD_TCP, ETH_IP6_UDP_GTP_IP4_TCP)
continue; continue;
} }
if ((72 <= i && i <= 73) || // skip IPv4 total length if ((72 <= i && i <= 73) || // skip IPv4 total length
(74 <= i && i <= 75) || // skip IPv4 identification
i == 78 || // skip IPv4 TTL
(80 <= i && i <= 81)) // skip IPv4 checksum (80 <= i && i <= 81)) // skip IPv4 checksum
{ {
continue; continue;
} }
if ((94 <= i && i <= 98) || // skip TCP seq if ((94 <= i && i <= 98) || // skip TCP seq
(99 <= i && i <= 103) || // skip TCP ack (99 <= i && i <= 103) || // skip TCP ack
i == 104 || // skip TCP data offset i == 104 || // skip TCP data offset
i == 105 || // skip TCP flags i == 105 || // skip TCP flags
(106 <= i && i <= 107) || // skip TCP window (106 <= i && i <= 107)) // skip TCP checksum
(108 <= i && i <= 109)) // skip TCP 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
/******************************************************************************
* [Protocols in frame: eth:ethertype:ip:gre:ipv6:tcp]
******************************************************************************
*
* Frame 1: 102 bytes on wire (816 bits), 102 bytes captured (816 bits)
* Ethernet II, Src: a0:b1:c2:d3:e4:f5 (a0:b1:c2:d3:e4:f5), Dst: CIMSYS_33:44:55 (00:11:22:33:44:55)
* Destination: CIMSYS_33:44:55 (00:11:22:33:44:55)
* Address: CIMSYS_33:44:55 (00:11:22:33:44:55)
* .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
* .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
* Source: a0:b1:c2:d3:e4:f5 (a0:b1:c2:d3:e4:f5)
* Address: a0:b1:c2:d3:e4:f5 (a0:b1:c2:d3:e4:f5)
* .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
* .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
* Type: IPv4 (0x0800)
* Internet Protocol Version 4, Src: 10.0.0.1, Dst: 192.168.1.1
* 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: 88
* Identification: 0x0001 (1)
* 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: 0xaecc [correct]
* [Header checksum status: Good]
* [Calculated Checksum: 0xaecc]
* Source Address: 10.0.0.1
* Destination Address: 192.168.1.1
* Generic Routing Encapsulation (IPv6)
* Flags and Version: 0x8000
* 1... .... .... .... = Checksum Bit: Yes
* .0.. .... .... .... = Routing Bit: No
* ..0. .... .... .... = Key Bit: No
* ...0 .... .... .... = Sequence Number Bit: No
* .... 0... .... .... = Strict Source Route Bit: No
* .... .000 .... .... = Recursion control: 0
* .... .... 0000 0... = Flags (Reserved): 0
* .... .... .... .000 = Version: GRE (0)
* Protocol Type: IPv6 (0x86dd)
* Checksum: 0x92e7 [correct]
* [Checksum Status: Good]
* Offset: 0
* Internet Protocol Version 6, Src: ::, Dst: 2001:db8::1
* 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: 20
* Next Header: TCP (6)
* Hop Limit: 64
* Source Address: ::
* Destination Address: 2001:db8::1
* Transmission Control Protocol, Src Port: 12345, Dst Port: 80, Seq: 0, Len: 0
* Source Port: 12345
* Destination Port: 80
* [Stream index: 0]
* [Conversation completeness: Incomplete, SYN_SENT (1)]
* ..0. .... = RST: Absent
* ...0 .... = FIN: Absent
* .... 0... = Data: Absent
* .... .0.. = ACK: Absent
* .... ..0. = SYN-ACK: Absent
* .... ...1 = SYN: Present
* [Completeness Flags: ·····S]
* [TCP Segment Len: 0]
* Sequence Number: 0
* [Next Sequence Number: 1]
* Acknowledgment Number: 0
* Acknowledgment number (raw): 0
* 0101 .... = Header Length: 20 bytes (5)
* Flags: 0x002 (SYN)
* 000. .... .... = Reserved: Not set
* ...0 .... .... = Accurate ECN: Not set
* .... 0... .... = Congestion Window Reduced: Not set
* .... .0.. .... = ECN-Echo: Not set
* .... ..0. .... = Urgent: Not set
* .... ...0 .... = Acknowledgment: Not set
* .... .... 0... = Push: Not set
* .... .... .0.. = Reset: Not set
* .... .... ..1. = Syn: Set
* [Expert Info (Chat/Sequence): Connection establish request (SYN): server port 80]
* [Connection establish request (SYN): server port 80]
* [Severity level: Chat]
* [Group: Sequence]
* .... .... ...0 = Fin: Not set
* [TCP Flags: ··········S·]
* Window: 8192
* [Calculated window size: 8192]
* Checksum: 0x31a0 [correct]
* [Calculated Checksum: 0x31a0]
* [Checksum Status: Good]
* Urgent Pointer: 0
* [Timestamps]
* [Time since first frame in this TCP stream: 0.000000000 seconds]
* [Time since previous frame in this TCP stream: 0.000000000 seconds]
*/
unsigned char data4[] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xa0, 0xb1, 0xc2, 0xd3, 0xe4, 0xf5, 0x08, 0x00, 0x45, 0x00, 0x00, 0x58, 0x00, 0x01, 0x00, 0x00, 0x40, 0x2f, 0xae, 0xcc,
0x0a, 0x00, 0x00, 0x01, 0xc0, 0xa8, 0x01, 0x01, 0x80, 0x00, 0x86, 0xdd, 0x92, 0xe7, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x14, 0x06, 0x40, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x30, 0x39, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02, 0x20, 0x00, 0x31, 0xa0, 0x00, 0x00};
/*
* craft_tcp_packet()
* -> ETH->IPv4->GRE->IPv6->TCP
* -> with TCP payload
* -> with GRE checksum
*/
#if 1
TEST(PACKET_CRAFT_TCP, ETH_IP4_GRE_IP6_TCP)
{
struct packet orig_pkt;
memset(&orig_pkt, 0, sizeof(orig_pkt));
packet_parse(&orig_pkt, (const char *)data4, sizeof(data4));
PRINT_GREEN("origin packet:");
packet_print(&orig_pkt);
struct packet *new_pkt = craft_tcp_packet(&orig_pkt, 1, 2, TH_ACK, NULL, 0, "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-ipv4-gre-ipv6-tcp.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 ==
new_pkt_len - 5); // trim TCP payload
struct layer layer;
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
{
if (layer.proto == LAYER_PROTO_IPV4)
{
const struct ip *ip = (const struct ip *)layer.hdr.raw;
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 93);
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0xaec7);
break;
}
if (layer.proto == LAYER_PROTO_GRE)
{
const struct gre0_hdr *gre = (const struct gre0_hdr *)layer.hdr.raw;
EXPECT_TRUE(gre0_hdr_get_version(gre) == 0);
EXPECT_TRUE(gre0_hdr_get_checksum(gre) == 0x92e7);
break;
}
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) == 25);
break;
}
if (layer.proto == LAYER_PROTO_TCP)
{
const struct tcphdr *tcp = (const struct tcphdr *)layer.hdr.raw;
EXPECT_TRUE(tcp_hdr_get_seq(tcp) == 1);
EXPECT_TRUE(tcp_hdr_get_ack(tcp) == 2);
EXPECT_TRUE(tcp_hdr_get_flags(tcp) == TH_ACK);
EXPECT_TRUE(tcp_hdr_get_hdr_len(tcp) == 20);
EXPECT_TRUE(tcp_hdr_get_checksum(tcp) == 0x0db8);
break;
}
}
for (uint16_t i = 0; i < new_pkt_len - 5; i++)
{
if ((16 <= i && i <= 17) || // skip IP total length
(24 <= i && i <= 25)) // skip IP checksum
{
continue;
}
if (38 <= i && i <= 39) // skip GRE checksum
{
continue;
}
if (47 <= i && i <= 48) // skip IPv6 payload length
{
continue;
}
if ((86 <= i && i <= 89) || // skip TCP seq
(90 <= i && i <= 93) || // skip TCP ack
i == 95 || // skip TCP flags
(98 <= i && i <= 99)) // skip TCP checksum
{ {
continue; continue;
} }

View File

@@ -14,8 +14,8 @@ global:
packet_get_raw_len; packet_get_raw_len;
packet_get_payload; packet_get_payload;
packet_get_payload_len; packet_get_payload_len;
imitate_tcp_packet; craft_tcp_packet;
imitate_udp_packet; craft_udp_packet;
craft_packet_from_scratch; craft_packet_from_scratch;
session_exdata_free; session_exdata_free;

View File

@@ -317,28 +317,28 @@ static inline uint32_t uint32_add(uint32_t seq, uint32_t inc)
return seq; return seq;
} }
static void imitate_and_send_udp_packet(struct stellar *st, struct session *sess, struct packet_exdata *pkt_exdata, static void craft_and_send_udp_packet(struct stellar *st, struct session *sess, struct packet_exdata *pkt_exdata,
enum flow_direction inject_dir, const char *udp_payload, uint16_t udp_payload_len) enum flow_direction inject_dir, const char *udp_payload, uint16_t udp_payload_len)
{ {
const struct packet *origin_pkt = session_get_first_packet(sess, inject_dir); const struct packet *origin_pkt = session_get_first_packet(sess, inject_dir);
if (origin_pkt == NULL) if (origin_pkt == NULL)
{ {
LOG_ERR("imitate UDP packet failed, %s origin packet is NULL\n", inject_dir == FLOW_DIRECTION_C2S ? "C2S" : "S2C"); LOG_ERR("craft UDP packet failed, %s origin packet is NULL\n", inject_dir == FLOW_DIRECTION_C2S ? "C2S" : "S2C");
return; return;
} }
struct packet *imitate_pkt = imitate_udp_packet(origin_pkt, udp_payload, udp_payload_len); struct packet *craft_pkt = craft_udp_packet(origin_pkt, udp_payload, udp_payload_len);
if (imitate_pkt == NULL) if (craft_pkt == NULL)
{ {
LOG_ERR("imitate UDP packet failed\n"); LOG_ERR("craft UDP packet failed\n");
return; return;
} }
stellar_send_crafted_packet(st, imitate_pkt); stellar_send_crafted_packet(st, craft_pkt);
} }
static void imitate_and_send_tcp_packet(struct stellar *st, struct session *sess, struct packet_exdata *pkt_exdata, static void craft_and_send_tcp_packet(struct stellar *st, struct session *sess, struct packet_exdata *pkt_exdata,
enum flow_direction inject_dir, uint8_t tcp_flags, const char *tcp_payload, uint16_t tcp_payload_len) enum flow_direction inject_dir, uint8_t tcp_flags, const char *tcp_payload, uint16_t tcp_payload_len)
{ {
uint32_t tcp_seq = 0; uint32_t tcp_seq = 0;
uint32_t tcp_ack = 0; uint32_t tcp_ack = 0;
@@ -384,18 +384,18 @@ static void imitate_and_send_tcp_packet(struct stellar *st, struct session *sess
const struct packet *origin_pkt = session_get_first_packet(sess, inject_dir); const struct packet *origin_pkt = session_get_first_packet(sess, inject_dir);
if (origin_pkt == NULL) if (origin_pkt == NULL)
{ {
LOG_ERR("imitate TCP packet failed, %s origin packet is NULL\n", inject_dir == FLOW_DIRECTION_C2S ? "C2S" : "S2C"); LOG_ERR("craft TCP packet failed, %s origin packet is NULL\n", inject_dir == FLOW_DIRECTION_C2S ? "C2S" : "S2C");
return; return;
} }
struct packet *imitate_pkt = imitate_tcp_packet(origin_pkt, tcp_seq, tcp_ack, tcp_flags, NULL, 0, tcp_payload, tcp_payload_len); struct packet *craft_pkt = craft_tcp_packet(origin_pkt, tcp_seq, tcp_ack, tcp_flags, NULL, 0, tcp_payload, tcp_payload_len);
if (imitate_pkt == NULL) if (craft_pkt == NULL)
{ {
LOG_ERR("imitate TCP packet failed\n"); LOG_ERR("craft TCP packet failed\n");
return; return;
} }
stellar_send_crafted_packet(st, imitate_pkt); stellar_send_crafted_packet(st, craft_pkt);
} }
/****************************************************************************** /******************************************************************************
@@ -480,36 +480,36 @@ static void on_sess_msg(struct session *sess, int topic_id, const void *msg, voi
switch (config->type) switch (config->type)
{ {
case INJECT_TCP_RST: case INJECT_TCP_RST:
imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0); craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0);
imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0); craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0);
session_set_discard(sess); session_set_discard(sess);
break; break;
case INJECT_TCP_FIN: case INJECT_TCP_FIN:
imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_FIN | TH_ACK, NULL, 0); craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_FIN | TH_ACK, NULL, 0);
imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_FIN | TH_ACK, NULL, 0); craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_FIN | TH_ACK, NULL, 0);
session_set_discard(sess); session_set_discard(sess);
break; break;
case INJECT_TCP_PAYLOAD: case INJECT_TCP_PAYLOAD:
snprintf(buffer, sizeof(buffer), "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s", 5 + 5 + 2, "Hello"); snprintf(buffer, sizeof(buffer), "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s", 5 + 5 + 2, "Hello");
imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, buffer, strlen(buffer)); // inject payload to client craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, buffer, strlen(buffer)); // inject payload to client
imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, "World\r\n", 7); // inject payload to client craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, "World\r\n", 7); // inject payload to client
imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0); // inject RST to client craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0); // inject RST to client
imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0); // inject RST to server craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0); // inject RST to server
session_set_discard(sess); session_set_discard(sess);
break; break;
case INJECT_TCP_PAYLOAD_FIN_RST: case INJECT_TCP_PAYLOAD_FIN_RST:
snprintf(buffer, sizeof(buffer), "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s", 5 + 5 + 2, "Hello"); snprintf(buffer, sizeof(buffer), "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s", 5 + 5 + 2, "Hello");
imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, buffer, strlen(buffer)); // inject payload to client craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, buffer, strlen(buffer)); // inject payload to client
imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, "World\r\n", 7); // inject payload to client craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, "World\r\n", 7); // inject payload to client
imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_FIN | TH_ACK, NULL, 0); // inject FIN to client craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_FIN | TH_ACK, NULL, 0); // inject FIN to client
imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0); // inject RST to client craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0); // inject RST to client
imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_FIN | TH_ACK, NULL, 0); // inject FIN to server craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_FIN | TH_ACK, NULL, 0); // inject FIN to server
imitate_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0); // inject RST to server craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0); // inject RST to server
session_set_discard(sess); session_set_discard(sess);
break; break;
case INJECT_UDP_PAYLOAD: case INJECT_UDP_PAYLOAD:
imitate_and_send_udp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, "Hello Server", 12); craft_and_send_udp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, "Hello Server", 12);
imitate_and_send_udp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, "Hello Client", 12); craft_and_send_udp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, "Hello Client", 12);
session_set_discard(sess); session_set_discard(sess);
break; break;
case INJECT_CTRL_MSG: case INJECT_CTRL_MSG: