refactor: update packet get layer/tunnel API
This commit is contained in:
@@ -70,26 +70,24 @@ struct layer
|
|||||||
};
|
};
|
||||||
|
|
||||||
int packet_get_layer_count(const struct packet *pkt);
|
int packet_get_layer_count(const struct packet *pkt);
|
||||||
// return 0: success
|
const struct layer *packet_get_layer_by_idx(const struct packet *pkt, int idx);
|
||||||
// return -1: failed
|
|
||||||
int packet_get_layer_by_idx(const struct packet *pkt, int idx, struct layer *out);
|
|
||||||
|
|
||||||
#define PACKET_FOREACH_LAYER_INORDER(pkt, layer) \
|
// // example: foreach layer in packet (inorder)
|
||||||
for (int i = 0; i < packet_get_layer_count(pkt) && packet_get_layer_by_idx(pkt, i, &layer) == 0; i++)
|
// int count = packet_get_layer_count(pkt);
|
||||||
|
// for (int i = 0; i < count; i++)
|
||||||
#define PACKET_FOREACH_LAYER_REVERSE(pkt, layer) \
|
// {
|
||||||
for (int i = packet_get_layer_count(pkt) - 1; i >= 0 && packet_get_layer_by_idx(pkt, i, &layer) == 0; i--)
|
// const struct layer *layer = packet_get_layer_by_idx(pkt, i);
|
||||||
|
// // do something with layer
|
||||||
#define PACKET_GETALL_LAYERS(pkt, layers) \
|
// }
|
||||||
({ \
|
//
|
||||||
memset(layers, 0, sizeof(layers)); \
|
//
|
||||||
int size = sizeof(layers) / sizeof(layers[0]); \
|
// // example: foreach layer in packet (reverse)
|
||||||
int count = packet_get_layer_count(pkt); \
|
// int count = packet_get_layer_count(pkt);
|
||||||
int num = count > size ? size : count; \
|
// for (int i = count - 1; i >= 0; i--)
|
||||||
for (int i = 0; i < num && packet_get_layer_by_idx(pkt, i, &layers[i]) == 0; i++) \
|
// {
|
||||||
/* void */; \
|
// const struct layer *layer = packet_get_layer_by_idx(pkt, i);
|
||||||
num; \
|
// // do something with layer
|
||||||
})
|
// }
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ struct tunnel
|
|||||||
enum tunnel_type type;
|
enum tunnel_type type;
|
||||||
|
|
||||||
int layer_count;
|
int layer_count;
|
||||||
struct layer layers[MAX_LAYERS_PER_TUNNEL];
|
const struct layer *layers[MAX_LAYERS_PER_TUNNEL];
|
||||||
};
|
};
|
||||||
|
|
||||||
int packet_get_tunnel_count(const struct packet *pkt);
|
int packet_get_tunnel_count(const struct packet *pkt);
|
||||||
@@ -41,23 +41,6 @@ int packet_get_tunnel_count(const struct packet *pkt);
|
|||||||
// return -1: failed
|
// return -1: failed
|
||||||
int packet_get_tunnel_by_idx(const struct packet *pkt, int idx, struct tunnel *out);
|
int packet_get_tunnel_by_idx(const struct packet *pkt, int idx, struct tunnel *out);
|
||||||
|
|
||||||
#define PACKET_FOREACH_TUNNEL_INORDER(pkt, tunnel) \
|
|
||||||
for (int i = 0; i < packet_get_tunnel_count(pkt) && packet_get_tunnel_by_idx(pkt, i, &tunnel) == 0; i++)
|
|
||||||
|
|
||||||
#define PACKET_FOREACH_TUNNEL_REVERSE(pkt, tunnel) \
|
|
||||||
for (int i = packet_get_tunnel_count(pkt) - 1; i >= 0 && packet_get_tunnel_by_idx(pkt, i, &tunnel) == 0; i--)
|
|
||||||
|
|
||||||
#define PACKET_GETALL_TUNNELS(pkt, tunnels) \
|
|
||||||
({ \
|
|
||||||
memset(tunnels, 0, sizeof(tunnels)); \
|
|
||||||
int size = sizeof(tunnels) / sizeof(tunnels[0]); \
|
|
||||||
int count = packet_get_tunnel_count(pkt); \
|
|
||||||
int num = count > size ? size : count; \
|
|
||||||
for (int i = 0; i < num && packet_get_tunnel_by_idx(pkt, i, &tunnels[i]) == 0; i++) \
|
|
||||||
/* void */; \
|
|
||||||
num; \
|
|
||||||
})
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -464,17 +464,16 @@ int packet_get_layer_count(const struct packet *pkt)
|
|||||||
return pkt->layers_used;
|
return pkt->layers_used;
|
||||||
}
|
}
|
||||||
|
|
||||||
int packet_get_layer_by_idx(const struct packet *pkt, int idx, struct layer *out)
|
const struct layer *packet_get_layer_by_idx(const struct packet *pkt, int idx)
|
||||||
{
|
{
|
||||||
const struct layer_private *raw = packet_get_layer(pkt, idx);
|
const struct layer_private *layer = packet_get_layer(pkt, idx);
|
||||||
if (raw == NULL)
|
if (layer == NULL)
|
||||||
{
|
{
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
layer_convert(raw, out);
|
return (const struct layer *)layer;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -681,11 +680,11 @@ int packet_get_tunnel_by_idx(const struct packet *pkt, int idx, struct tunnel *o
|
|||||||
out->type = hit->type;
|
out->type = hit->type;
|
||||||
out->layer_count = hit->contain_layers;
|
out->layer_count = hit->contain_layers;
|
||||||
if (out->layer_count >= 1)
|
if (out->layer_count >= 1)
|
||||||
layer_convert(curr, &out->layers[0]);
|
out->layers[0] = (const struct layer *)curr;
|
||||||
if (out->layer_count >= 2)
|
if (out->layer_count >= 2)
|
||||||
layer_convert(next1, &out->layers[1]);
|
out->layers[1] = (const struct layer *)next1;
|
||||||
if (out->layer_count >= 3)
|
if (out->layer_count >= 3)
|
||||||
layer_convert(next2, &out->layers[2]);
|
out->layers[2] = (const struct layer *)next2;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -922,15 +921,3 @@ int packet_is_fragment(const struct packet *pkt)
|
|||||||
{
|
{
|
||||||
return (pkt->frag_layer) ? 1 : 0;
|
return (pkt->frag_layer) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void layer_convert(const struct layer_private *in, struct layer *out)
|
|
||||||
{
|
|
||||||
if (in == NULL || out == NULL)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
out->proto = in->proto;
|
|
||||||
out->hdr_len = in->hdr_len;
|
|
||||||
out->hdr.raw = (char *)in->hdr_ptr;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -36,11 +36,14 @@ struct metadata
|
|||||||
struct layer_private
|
struct layer_private
|
||||||
{
|
{
|
||||||
enum layer_proto proto;
|
enum layer_proto proto;
|
||||||
const char *hdr_ptr; // header pointer
|
|
||||||
const char *pld_ptr; // payload pointer
|
|
||||||
uint16_t hdr_offset; // header offset from data_ptr
|
|
||||||
uint16_t hdr_len; // header length
|
uint16_t hdr_len; // header length
|
||||||
|
const char *hdr_ptr; // header pointer
|
||||||
|
|
||||||
uint16_t pld_len; // payload length
|
uint16_t pld_len; // payload length
|
||||||
|
const char *pld_ptr; // payload pointer
|
||||||
|
|
||||||
|
uint16_t hdr_offset; // header offset from data_ptr
|
||||||
};
|
};
|
||||||
|
|
||||||
struct packet
|
struct packet
|
||||||
@@ -140,7 +143,6 @@ struct packet *packet_dup(const struct packet *pkt);
|
|||||||
void packet_free(struct packet *pkt);
|
void packet_free(struct packet *pkt);
|
||||||
|
|
||||||
int packet_is_fragment(const struct packet *pkt);
|
int packet_is_fragment(const struct packet *pkt);
|
||||||
void layer_convert(const struct layer_private *in, struct layer *out);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -145,19 +145,20 @@ TEST(PACKET_BUILD_TCP, ETH_IP4_TCP)
|
|||||||
|
|
||||||
EXPECT_TRUE(orig_pkt_len - 6 == // trim Eth padding: 000000000000
|
EXPECT_TRUE(orig_pkt_len - 6 == // trim Eth padding: 000000000000
|
||||||
new_pkt_len - 12 - 5); // trim TCP options, TCP payload
|
new_pkt_len - 12 - 5); // trim TCP options, TCP payload
|
||||||
struct layer layer;
|
int count = packet_get_layer_count(new_pkt);
|
||||||
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
if (layer.proto == LAYER_PROTO_IPV4)
|
const struct layer *layer = packet_get_layer_by_idx(new_pkt, i);
|
||||||
|
if (layer->proto == LAYER_PROTO_IPV4)
|
||||||
{
|
{
|
||||||
const struct ip *ip = (const 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);
|
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 = (const 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);
|
||||||
@@ -282,25 +283,26 @@ TEST(PACKET_BUILD_TCP, ETH_IP4_IP6_TCP)
|
|||||||
|
|
||||||
EXPECT_TRUE(orig_pkt_len - 12 == // trim TCP options
|
EXPECT_TRUE(orig_pkt_len - 12 == // trim TCP options
|
||||||
new_pkt_len - 5); // trim TCP payload
|
new_pkt_len - 5); // trim TCP payload
|
||||||
struct layer layer;
|
int count = packet_get_layer_count(new_pkt);
|
||||||
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
if (layer.proto == LAYER_PROTO_IPV4)
|
const struct layer *layer = packet_get_layer_by_idx(new_pkt, i);
|
||||||
|
if (layer->proto == LAYER_PROTO_IPV4)
|
||||||
{
|
{
|
||||||
const struct ip *ip = (const 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);
|
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 = (const 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 = (const 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);
|
||||||
@@ -498,39 +500,40 @@ TEST(PACKET_BUILD_TCP, ETH_IP6_UDP_GTP_IP4_TCP)
|
|||||||
|
|
||||||
EXPECT_TRUE(orig_pkt_len - 12 - 1348 == // trim TCP options, TCP payload
|
EXPECT_TRUE(orig_pkt_len - 12 - 1348 == // trim TCP options, TCP payload
|
||||||
new_pkt_len - 5); // trim TCP payload
|
new_pkt_len - 5); // trim TCP payload
|
||||||
struct layer layer;
|
int count = packet_get_layer_count(new_pkt);
|
||||||
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
if (layer.proto == LAYER_PROTO_IPV6)
|
const struct layer *layer = packet_get_layer_by_idx(new_pkt, i);
|
||||||
|
if (layer->proto == LAYER_PROTO_IPV6)
|
||||||
{
|
{
|
||||||
const struct ip6_hdr *ip6 = (const 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 = (const 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);
|
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 = (const 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 = (const 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);
|
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 = (const 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);
|
||||||
@@ -719,32 +722,33 @@ TEST(PACKET_BUILD_TCP, ETH_IP4_GRE_IP6_TCP)
|
|||||||
|
|
||||||
EXPECT_TRUE(orig_pkt_len ==
|
EXPECT_TRUE(orig_pkt_len ==
|
||||||
new_pkt_len - 5); // trim TCP payload
|
new_pkt_len - 5); // trim TCP payload
|
||||||
struct layer layer;
|
int count = packet_get_layer_count(new_pkt);
|
||||||
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
if (layer.proto == LAYER_PROTO_IPV4)
|
const struct layer *layer = packet_get_layer_by_idx(new_pkt, i);
|
||||||
|
if (layer->proto == LAYER_PROTO_IPV4)
|
||||||
{
|
{
|
||||||
const struct ip *ip = (const struct ip *)layer.hdr.raw;
|
const struct ip *ip = (const struct ip *)layer->hdr.raw;
|
||||||
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 93);
|
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 93);
|
||||||
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0xaec7);
|
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0xaec7);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (layer.proto == LAYER_PROTO_GRE)
|
if (layer->proto == LAYER_PROTO_GRE)
|
||||||
{
|
{
|
||||||
const struct gre0_hdr *gre = (const struct gre0_hdr *)layer.hdr.raw;
|
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_version(gre) == 0);
|
||||||
EXPECT_TRUE(gre0_hdr_get_checksum(gre) == 0x92e7);
|
EXPECT_TRUE(gre0_hdr_get_checksum(gre) == 0x92e7);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (layer.proto == LAYER_PROTO_IPV6)
|
if (layer->proto == LAYER_PROTO_IPV6)
|
||||||
{
|
{
|
||||||
const struct ip6_hdr *ip6 = (const 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 = (const 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);
|
||||||
@@ -946,19 +950,20 @@ TEST(PACKET_BUILD_UDP, ETH_VLAN_IPv6_IPv4_GRE_PPP_IPv4_UDP_DNS)
|
|||||||
|
|
||||||
EXPECT_TRUE(orig_pkt_len - 71 == // trim DNS payload
|
EXPECT_TRUE(orig_pkt_len - 71 == // trim DNS payload
|
||||||
new_pkt_len - 5); // trim UDP payload
|
new_pkt_len - 5); // trim UDP payload
|
||||||
int count = 0;
|
int flag = 0;
|
||||||
struct layer layer;
|
int count = packet_get_layer_count(new_pkt);
|
||||||
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
if (layer.proto == LAYER_PROTO_IPV6)
|
const struct layer *layer = packet_get_layer_by_idx(new_pkt, i);
|
||||||
|
if (layer->proto == LAYER_PROTO_IPV6)
|
||||||
{
|
{
|
||||||
const struct ip6_hdr *ip6 = (const 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) == 73);
|
EXPECT_TRUE(ip6_hdr_get_payload_len(ip6) == 73);
|
||||||
}
|
}
|
||||||
if (layer.proto == LAYER_PROTO_IPV4)
|
if (layer->proto == LAYER_PROTO_IPV4)
|
||||||
{
|
{
|
||||||
const struct ip *ip = (const struct ip *)layer.hdr.raw;
|
const struct ip *ip = (const struct ip *)layer->hdr.raw;
|
||||||
if (count == 0)
|
if (flag == 0)
|
||||||
{
|
{
|
||||||
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 73);
|
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 73);
|
||||||
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x7640);
|
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x7640);
|
||||||
@@ -968,17 +973,17 @@ TEST(PACKET_BUILD_UDP, ETH_VLAN_IPv6_IPv4_GRE_PPP_IPv4_UDP_DNS)
|
|||||||
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 33);
|
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 33);
|
||||||
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x56a9);
|
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x56a9);
|
||||||
}
|
}
|
||||||
count++;
|
flag++;
|
||||||
}
|
}
|
||||||
if (layer.proto == LAYER_PROTO_GRE)
|
if (layer->proto == LAYER_PROTO_GRE)
|
||||||
{
|
{
|
||||||
const struct gre1_hdr *gre = (const struct gre1_hdr *)layer.hdr.raw;
|
const struct gre1_hdr *gre = (const struct gre1_hdr *)layer->hdr.raw;
|
||||||
EXPECT_TRUE(gre1_hdr_get_version(gre) == 1);
|
EXPECT_TRUE(gre1_hdr_get_version(gre) == 1);
|
||||||
EXPECT_TRUE(gre1_hdr_get_payload_length(gre) == 37);
|
EXPECT_TRUE(gre1_hdr_get_payload_length(gre) == 37);
|
||||||
}
|
}
|
||||||
if (layer.proto == LAYER_PROTO_UDP)
|
if (layer->proto == LAYER_PROTO_UDP)
|
||||||
{
|
{
|
||||||
const struct udphdr *udp = (const struct udphdr *)layer.hdr.raw;
|
const struct udphdr *udp = (const struct udphdr *)layer->hdr.raw;
|
||||||
EXPECT_TRUE(udp_hdr_get_total_len(udp) == 13);
|
EXPECT_TRUE(udp_hdr_get_total_len(udp) == 13);
|
||||||
EXPECT_TRUE(udp_hdr_get_checksum(udp) == 0x5469);
|
EXPECT_TRUE(udp_hdr_get_checksum(udp) == 0x5469);
|
||||||
}
|
}
|
||||||
@@ -1074,17 +1079,18 @@ TEST(PACKET_BUILD_L3, ETH_IP4_ICMP)
|
|||||||
|
|
||||||
EXPECT_TRUE(orig_pkt_len - 20 - 6 == // trim Eth padding, trim TCP header
|
EXPECT_TRUE(orig_pkt_len - 20 - 6 == // trim Eth padding, trim TCP header
|
||||||
new_pkt_len - 64); // trim ICMP
|
new_pkt_len - 64); // trim ICMP
|
||||||
struct layer layer;
|
int count = packet_get_layer_count(new_pkt);
|
||||||
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
if (layer.proto == LAYER_PROTO_IPV4)
|
const struct layer *layer = packet_get_layer_by_idx(new_pkt, i);
|
||||||
|
if (layer->proto == LAYER_PROTO_IPV4)
|
||||||
{
|
{
|
||||||
const struct ip *ip = (const struct ip *)layer.hdr.raw;
|
const struct ip *ip = (const struct ip *)layer->hdr.raw;
|
||||||
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 84);
|
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 84);
|
||||||
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0xb7cb);
|
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0xb7cb);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (layer.proto == LAYER_PROTO_ICMP)
|
if (layer->proto == LAYER_PROTO_ICMP)
|
||||||
{
|
{
|
||||||
// TODO
|
// TODO
|
||||||
break;
|
break;
|
||||||
@@ -1142,12 +1148,13 @@ TEST(PACKET_BUILD_L3, ETH_IP6_ICMP)
|
|||||||
struct icmp6_hdr *icmp = (struct icmp6_hdr *)icmp_resp;
|
struct icmp6_hdr *icmp = (struct icmp6_hdr *)icmp_resp;
|
||||||
icmp->icmp6_cksum = 0;
|
icmp->icmp6_cksum = 0;
|
||||||
|
|
||||||
struct layer layer;
|
int count = packet_get_layer_count(&orig_pkt);
|
||||||
PACKET_FOREACH_LAYER_REVERSE(&orig_pkt, layer)
|
for (int i = count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
if (layer.proto == LAYER_PROTO_IPV6)
|
const struct layer *layer = packet_get_layer_by_idx(&orig_pkt, i);
|
||||||
|
if (layer->proto == LAYER_PROTO_IPV6)
|
||||||
{
|
{
|
||||||
struct ip6_hdr *ip6 = (struct ip6_hdr *)layer.hdr.raw;
|
struct ip6_hdr *ip6 = (struct ip6_hdr *)layer->hdr.raw;
|
||||||
icmp->icmp6_cksum = checksum_v6(icmp, sizeof(icmp_resp), IPPROTO_ICMPV6, &ip6->ip6_src, &ip6->ip6_dst);
|
icmp->icmp6_cksum = checksum_v6(icmp, sizeof(icmp_resp), IPPROTO_ICMPV6, &ip6->ip6_src, &ip6->ip6_dst);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1169,22 +1176,24 @@ TEST(PACKET_BUILD_L3, ETH_IP6_ICMP)
|
|||||||
|
|
||||||
EXPECT_TRUE(orig_pkt_len - 32 == // trim TCP header
|
EXPECT_TRUE(orig_pkt_len - 32 == // trim TCP header
|
||||||
new_pkt_len - 60); // trim ICMPv6 header
|
new_pkt_len - 60); // trim ICMPv6 header
|
||||||
PACKET_FOREACH_LAYER_INORDER(new_pkt, layer)
|
count = packet_get_layer_count(new_pkt);
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
if (layer.proto == LAYER_PROTO_IPV4)
|
const struct layer *layer = packet_get_layer_by_idx(new_pkt, i);
|
||||||
|
if (layer->proto == LAYER_PROTO_IPV4)
|
||||||
{
|
{
|
||||||
const struct ip *ip = (const struct ip *)layer.hdr.raw;
|
const struct ip *ip = (const struct ip *)layer->hdr.raw;
|
||||||
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 120);
|
EXPECT_TRUE(ip4_hdr_get_total_len(ip) == 120);
|
||||||
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x09ac);
|
EXPECT_TRUE(ip4_hdr_get_checksum(ip) == 0x09ac);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (layer.proto == LAYER_PROTO_IPV6)
|
if (layer->proto == LAYER_PROTO_IPV6)
|
||||||
{
|
{
|
||||||
const struct ip6_hdr *ip6 = (const 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) == 60);
|
EXPECT_TRUE(ip6_hdr_get_payload_len(ip6) == 60);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (layer.proto == LAYER_PROTO_ICMP6)
|
if (layer->proto == LAYER_PROTO_ICMP6)
|
||||||
{
|
{
|
||||||
// TODO
|
// TODO
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -555,8 +555,8 @@ TEST(TUNNEL, IPV4)
|
|||||||
EXPECT_TRUE(out.type == TUNNEL_IPV4);
|
EXPECT_TRUE(out.type == TUNNEL_IPV4);
|
||||||
EXPECT_TRUE(out.layer_count == 1);
|
EXPECT_TRUE(out.layer_count == 1);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[0].proto == LAYER_PROTO_IPV4);
|
EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4);
|
||||||
EXPECT_TRUE(out.layers[0].hdr_len == 20);
|
EXPECT_TRUE(out.layers[0]->hdr_len == 20);
|
||||||
|
|
||||||
// No tunnel
|
// No tunnel
|
||||||
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
|
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
|
||||||
@@ -583,19 +583,19 @@ TEST(TUNNEL, GRE)
|
|||||||
EXPECT_TRUE(out.type == TUNNEL_IPV6);
|
EXPECT_TRUE(out.type == TUNNEL_IPV6);
|
||||||
EXPECT_TRUE(out.layer_count == 1);
|
EXPECT_TRUE(out.layer_count == 1);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[0].proto == LAYER_PROTO_IPV6);
|
EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV6);
|
||||||
EXPECT_TRUE(out.layers[0].hdr_len == 40);
|
EXPECT_TRUE(out.layers[0]->hdr_len == 40);
|
||||||
|
|
||||||
// GRE tunnel
|
// GRE tunnel
|
||||||
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == 0);
|
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == 0);
|
||||||
EXPECT_TRUE(out.type == TUNNEL_GRE);
|
EXPECT_TRUE(out.type == TUNNEL_GRE);
|
||||||
EXPECT_TRUE(out.layer_count == 2);
|
EXPECT_TRUE(out.layer_count == 2);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[0].proto == LAYER_PROTO_IPV4);
|
EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4);
|
||||||
EXPECT_TRUE(out.layers[0].hdr_len == 20);
|
EXPECT_TRUE(out.layers[0]->hdr_len == 20);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[1].proto == LAYER_PROTO_GRE);
|
EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_GRE);
|
||||||
EXPECT_TRUE(out.layers[1].hdr_len == 16);
|
EXPECT_TRUE(out.layers[1]->hdr_len == 16);
|
||||||
|
|
||||||
// No tunnel
|
// No tunnel
|
||||||
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 2, &out) == -1);
|
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 2, &out) == -1);
|
||||||
@@ -617,14 +617,14 @@ TEST(TUNNEL, GTP)
|
|||||||
EXPECT_TRUE(out.type == TUNNEL_GTP);
|
EXPECT_TRUE(out.type == TUNNEL_GTP);
|
||||||
EXPECT_TRUE(out.layer_count == 3);
|
EXPECT_TRUE(out.layer_count == 3);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[0].proto == LAYER_PROTO_IPV6);
|
EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV6);
|
||||||
EXPECT_TRUE(out.layers[0].hdr_len == 40);
|
EXPECT_TRUE(out.layers[0]->hdr_len == 40);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[1].proto == LAYER_PROTO_UDP);
|
EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_UDP);
|
||||||
EXPECT_TRUE(out.layers[1].hdr_len == 8);
|
EXPECT_TRUE(out.layers[1]->hdr_len == 8);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[2].proto == LAYER_PROTO_GTP_U);
|
EXPECT_TRUE(out.layers[2]->proto == LAYER_PROTO_GTP_U);
|
||||||
EXPECT_TRUE(out.layers[2].hdr_len == 8);
|
EXPECT_TRUE(out.layers[2]->hdr_len == 8);
|
||||||
|
|
||||||
// No tunnel
|
// No tunnel
|
||||||
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
|
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
|
||||||
@@ -646,14 +646,14 @@ TEST(TUNNEL, VXLAN)
|
|||||||
EXPECT_TRUE(out.type == TUNNEL_VXLAN);
|
EXPECT_TRUE(out.type == TUNNEL_VXLAN);
|
||||||
EXPECT_TRUE(out.layer_count == 3);
|
EXPECT_TRUE(out.layer_count == 3);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[0].proto == LAYER_PROTO_IPV4);
|
EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4);
|
||||||
EXPECT_TRUE(out.layers[0].hdr_len == 20);
|
EXPECT_TRUE(out.layers[0]->hdr_len == 20);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[1].proto == LAYER_PROTO_UDP);
|
EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_UDP);
|
||||||
EXPECT_TRUE(out.layers[1].hdr_len == 8);
|
EXPECT_TRUE(out.layers[1]->hdr_len == 8);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[2].proto == LAYER_PROTO_VXLAN);
|
EXPECT_TRUE(out.layers[2]->proto == LAYER_PROTO_VXLAN);
|
||||||
EXPECT_TRUE(out.layers[2].hdr_len == 8);
|
EXPECT_TRUE(out.layers[2]->hdr_len == 8);
|
||||||
|
|
||||||
// No tunnel
|
// No tunnel
|
||||||
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
|
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
|
||||||
@@ -675,14 +675,14 @@ TEST(TUNNEL, L2TP)
|
|||||||
EXPECT_TRUE(out.type == TUNNEL_L2TP);
|
EXPECT_TRUE(out.type == TUNNEL_L2TP);
|
||||||
EXPECT_TRUE(out.layer_count == 3);
|
EXPECT_TRUE(out.layer_count == 3);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[0].proto == LAYER_PROTO_IPV4);
|
EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4);
|
||||||
EXPECT_TRUE(out.layers[0].hdr_len == 20);
|
EXPECT_TRUE(out.layers[0]->hdr_len == 20);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[1].proto == LAYER_PROTO_UDP);
|
EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_UDP);
|
||||||
EXPECT_TRUE(out.layers[1].hdr_len == 8);
|
EXPECT_TRUE(out.layers[1]->hdr_len == 8);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[2].proto == LAYER_PROTO_L2TP);
|
EXPECT_TRUE(out.layers[2]->proto == LAYER_PROTO_L2TP);
|
||||||
EXPECT_TRUE(out.layers[2].hdr_len == 8);
|
EXPECT_TRUE(out.layers[2]->hdr_len == 8);
|
||||||
|
|
||||||
// No tunnel
|
// No tunnel
|
||||||
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
|
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
|
||||||
@@ -704,11 +704,11 @@ TEST(TUNNEL, TEREDO)
|
|||||||
EXPECT_TRUE(out.type == TUNNEL_TEREDO);
|
EXPECT_TRUE(out.type == TUNNEL_TEREDO);
|
||||||
EXPECT_TRUE(out.layer_count == 2);
|
EXPECT_TRUE(out.layer_count == 2);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[0].proto == LAYER_PROTO_IPV4);
|
EXPECT_TRUE(out.layers[0]->proto == LAYER_PROTO_IPV4);
|
||||||
EXPECT_TRUE(out.layers[0].hdr_len == 20);
|
EXPECT_TRUE(out.layers[0]->hdr_len == 20);
|
||||||
|
|
||||||
EXPECT_TRUE(out.layers[1].proto == LAYER_PROTO_UDP);
|
EXPECT_TRUE(out.layers[1]->proto == LAYER_PROTO_UDP);
|
||||||
EXPECT_TRUE(out.layers[1].hdr_len == 8);
|
EXPECT_TRUE(out.layers[1]->hdr_len == 8);
|
||||||
|
|
||||||
// No tunnel
|
// No tunnel
|
||||||
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
|
EXPECT_TRUE(packet_get_tunnel_by_idx(&pkt, 1, &out) == -1);
|
||||||
|
|||||||
@@ -270,31 +270,32 @@ static inline void packet_exdata_init(const struct packet *pkt, enum flow_direct
|
|||||||
pkt_exdata->flow_dir = dir;
|
pkt_exdata->flow_dir = dir;
|
||||||
|
|
||||||
int get_inner_addr = 0;
|
int get_inner_addr = 0;
|
||||||
struct layer layer;
|
int count = packet_get_layer_count(pkt);
|
||||||
PACKET_FOREACH_LAYER_REVERSE(pkt, layer)
|
for (int i = count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
switch (layer.proto)
|
const struct layer *layer = packet_get_layer_by_idx(pkt, i);
|
||||||
|
switch (layer->proto)
|
||||||
{
|
{
|
||||||
case LAYER_PROTO_TCP:
|
case LAYER_PROTO_TCP:
|
||||||
pkt_exdata->src_port = ntohs(layer.hdr.tcp->th_sport);
|
pkt_exdata->src_port = ntohs(layer->hdr.tcp->th_sport);
|
||||||
pkt_exdata->dst_port = ntohs(layer.hdr.tcp->th_dport);
|
pkt_exdata->dst_port = ntohs(layer->hdr.tcp->th_dport);
|
||||||
pkt_exdata->tcp_seq = ntohl(layer.hdr.tcp->th_seq);
|
pkt_exdata->tcp_seq = ntohl(layer->hdr.tcp->th_seq);
|
||||||
pkt_exdata->tcp_ack = ntohl(layer.hdr.tcp->th_ack);
|
pkt_exdata->tcp_ack = ntohl(layer->hdr.tcp->th_ack);
|
||||||
pkt_exdata->tcp_flags = layer.hdr.tcp->th_flags;
|
pkt_exdata->tcp_flags = layer->hdr.tcp->th_flags;
|
||||||
pkt_exdata->tcp_payload_len = packet_get_payload_len(pkt);
|
pkt_exdata->tcp_payload_len = packet_get_payload_len(pkt);
|
||||||
break;
|
break;
|
||||||
case LAYER_PROTO_UDP:
|
case LAYER_PROTO_UDP:
|
||||||
pkt_exdata->src_port = ntohs(layer.hdr.udp->uh_sport);
|
pkt_exdata->src_port = ntohs(layer->hdr.udp->uh_sport);
|
||||||
pkt_exdata->dst_port = ntohs(layer.hdr.udp->uh_dport);
|
pkt_exdata->dst_port = ntohs(layer->hdr.udp->uh_dport);
|
||||||
break;
|
break;
|
||||||
case LAYER_PROTO_IPV4:
|
case LAYER_PROTO_IPV4:
|
||||||
pkt_exdata->src_addr.v4 = layer.hdr.ip4->ip_src;
|
pkt_exdata->src_addr.v4 = layer->hdr.ip4->ip_src;
|
||||||
pkt_exdata->dst_addr.v4 = layer.hdr.ip4->ip_dst;
|
pkt_exdata->dst_addr.v4 = layer->hdr.ip4->ip_dst;
|
||||||
get_inner_addr = 1;
|
get_inner_addr = 1;
|
||||||
break;
|
break;
|
||||||
case LAYER_PROTO_IPV6:
|
case LAYER_PROTO_IPV6:
|
||||||
pkt_exdata->src_addr.v6 = layer.hdr.ip6->ip6_src;
|
pkt_exdata->src_addr.v6 = layer->hdr.ip6->ip6_src;
|
||||||
pkt_exdata->dst_addr.v6 = layer.hdr.ip6->ip6_dst;
|
pkt_exdata->dst_addr.v6 = layer->hdr.ip6->ip6_dst;
|
||||||
get_inner_addr = 1;
|
get_inner_addr = 1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user