rename flow_direction to flow_type
This commit is contained in:
@@ -36,9 +36,9 @@ struct config
|
||||
struct sockaddr_in6 v6;
|
||||
} addr;
|
||||
uint16_t port;
|
||||
uint64_t number; // inject packet after (C2S/S2C) direction receiving n packets
|
||||
enum inject_type type;
|
||||
enum flow_direction direction;
|
||||
uint64_t number; // inject packet after (C2S/S2C) flow_type receiving n packets
|
||||
enum inject_type inject_type;
|
||||
enum flow_type flow_type;
|
||||
};
|
||||
|
||||
struct inject_plugin_ctx
|
||||
@@ -53,7 +53,7 @@ struct inject_plugin_ctx
|
||||
|
||||
struct packet_exdata
|
||||
{
|
||||
enum flow_direction flow_dir;
|
||||
enum flow_type flow_type;
|
||||
|
||||
union
|
||||
{
|
||||
@@ -161,23 +161,23 @@ static int load_config(struct logger *logger, struct config *config, const char
|
||||
}
|
||||
config->port = atoi(ptr);
|
||||
|
||||
ptr = toml_raw_in(sub, "filter_dir");
|
||||
ptr = toml_raw_in(sub, "flow_type");
|
||||
if (ptr == NULL)
|
||||
{
|
||||
INJTECT_PLUGIN_LOG_ERROR(logger, "config file missing packet_inject->filter_dir");
|
||||
INJTECT_PLUGIN_LOG_ERROR(logger, "config file missing packet_inject->flow_type");
|
||||
goto error_out;
|
||||
}
|
||||
if (strcmp(ptr, "C2S") == 0)
|
||||
{
|
||||
config->direction = FLOW_DIRECTION_C2S;
|
||||
config->flow_type = FLOW_TYPE_C2S;
|
||||
}
|
||||
else if (strcmp(ptr, "S2C") == 0)
|
||||
{
|
||||
config->direction = FLOW_DIRECTION_S2C;
|
||||
config->flow_type = FLOW_TYPE_S2C;
|
||||
}
|
||||
else
|
||||
{
|
||||
INJTECT_PLUGIN_LOG_ERROR(logger, "parse packet_inject->filter_dir failed, invalid direction: %s", ptr);
|
||||
INJTECT_PLUGIN_LOG_ERROR(logger, "parse packet_inject->flow_type failed, invalid flow_type: %s", ptr);
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
@@ -202,31 +202,31 @@ static int load_config(struct logger *logger, struct config *config, const char
|
||||
}
|
||||
if (strcmp(ptr, "TCP-RST") == 0)
|
||||
{
|
||||
config->type = INJECT_TCP_RST;
|
||||
config->inject_type = INJECT_TCP_RST;
|
||||
}
|
||||
else if (strcmp(ptr, "TCP-FIN") == 0)
|
||||
{
|
||||
config->type = INJECT_TCP_FIN;
|
||||
config->inject_type = INJECT_TCP_FIN;
|
||||
}
|
||||
else if (strcmp(ptr, "TCP-PAYLOAD") == 0)
|
||||
{
|
||||
config->type = INJECT_TCP_PAYLOAD;
|
||||
config->inject_type = INJECT_TCP_PAYLOAD;
|
||||
}
|
||||
else if (strcmp(ptr, "TCP-PAYLOAD-FIN-RST") == 0)
|
||||
{
|
||||
config->type = INJECT_TCP_PAYLOAD_FIN_RST;
|
||||
config->inject_type = INJECT_TCP_PAYLOAD_FIN_RST;
|
||||
}
|
||||
else if (strcmp(ptr, "UDP-PAYLOAD") == 0)
|
||||
{
|
||||
config->type = INJECT_UDP_PAYLOAD;
|
||||
config->inject_type = INJECT_UDP_PAYLOAD;
|
||||
}
|
||||
else if (strcmp(ptr, "CTRL-MSG") == 0)
|
||||
{
|
||||
config->type = INJECT_CTRL_MSG;
|
||||
config->inject_type = INJECT_CTRL_MSG;
|
||||
}
|
||||
else
|
||||
{
|
||||
INJTECT_PLUGIN_LOG_ERROR(logger, "parse packet_inject->inject_type failed, invalid inject type: %s", ptr);
|
||||
INJTECT_PLUGIN_LOG_ERROR(logger, "parse packet_inject->inject_type failed, invalid inject_type: %s", ptr);
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
@@ -265,20 +265,20 @@ static void print_config(struct logger *logger, const struct config *config)
|
||||
|
||||
INJTECT_PLUGIN_LOG_INFO(logger, "config->filter_ip : %s", addr_str);
|
||||
INJTECT_PLUGIN_LOG_INFO(logger, "config->filter_port : %d", config->port);
|
||||
INJTECT_PLUGIN_LOG_INFO(logger, "config->filter_dir : %s", config->direction == FLOW_DIRECTION_C2S ? "C2S" : "S2C");
|
||||
INJTECT_PLUGIN_LOG_INFO(logger, "config->flow_type : %s", config->flow_type == FLOW_TYPE_C2S ? "C2S" : "S2C");
|
||||
INJTECT_PLUGIN_LOG_INFO(logger, "config->filter_pkts : %lu", config->number);
|
||||
INJTECT_PLUGIN_LOG_INFO(logger, "config->inject_type : %s", type_to_str(config->type));
|
||||
INJTECT_PLUGIN_LOG_INFO(logger, "config->inject_type : %s", type_to_str(config->inject_type));
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Utils
|
||||
******************************************************************************/
|
||||
|
||||
static inline void packet_exdata_init(const struct packet *pkt, enum flow_direction dir, struct packet_exdata *pkt_exdata)
|
||||
static inline void packet_exdata_init(const struct packet *pkt, enum flow_type type, struct packet_exdata *pkt_exdata)
|
||||
{
|
||||
memset(pkt_exdata, 0, sizeof(struct packet_exdata));
|
||||
|
||||
pkt_exdata->flow_dir = dir;
|
||||
pkt_exdata->flow_type = type;
|
||||
|
||||
int get_inner_addr = 0;
|
||||
int count = packet_get_layer_count(pkt);
|
||||
@@ -335,12 +335,12 @@ static inline uint32_t uint32_add(uint32_t seq, uint32_t inc)
|
||||
}
|
||||
|
||||
static void build_and_send_udp_packet(struct inject_plugin_ctx *ctx, struct session *sess, struct packet_exdata *pkt_exdata,
|
||||
enum flow_direction inject_dir, const char *udp_payload, uint16_t udp_payload_len)
|
||||
enum flow_type type, 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, type);
|
||||
if (origin_pkt == NULL)
|
||||
{
|
||||
INJTECT_PLUGIN_LOG_ERROR(ctx->logger, "build UDP packet failed, %s origin packet is NULL", inject_dir == FLOW_DIRECTION_C2S ? "C2S" : "S2C");
|
||||
INJTECT_PLUGIN_LOG_ERROR(ctx->logger, "build UDP packet failed, %s origin packet is NULL", type == FLOW_TYPE_C2S ? "C2S" : "S2C");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -355,7 +355,7 @@ static void build_and_send_udp_packet(struct inject_plugin_ctx *ctx, struct sess
|
||||
}
|
||||
|
||||
static void build_and_send_tcp_packet(struct inject_plugin_ctx *ctx, 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_type type, uint8_t tcp_flags, const char *tcp_payload, uint16_t tcp_payload_len)
|
||||
{
|
||||
uint32_t tcp_seq = 0;
|
||||
uint32_t tcp_ack = 0;
|
||||
@@ -369,11 +369,11 @@ static void build_and_send_tcp_packet(struct inject_plugin_ctx *ctx, struct sess
|
||||
*
|
||||
* for example: current packet is C2S
|
||||
*
|
||||
* inject direction == current direction (inject C2S RST)
|
||||
* inject flow_type == current flow_type (inject C2S RST)
|
||||
* tcp_seq = current_packet_seq
|
||||
* tcp_ack = current_packet_ack
|
||||
*
|
||||
* inject direction != current direction (inject S2C RST)
|
||||
* inject flow_type != current flow_type (inject S2C RST)
|
||||
* tcp_seq = current_packet_ack
|
||||
* tcp_ack = current_packet_seq + current_packet_payload_len
|
||||
* or if current packet is a SYN-ACK packet
|
||||
@@ -381,7 +381,7 @@ static void build_and_send_tcp_packet(struct inject_plugin_ctx *ctx, struct sess
|
||||
* tcp_ack = current_packet_ack + current_packet_payload_len + 1
|
||||
*/
|
||||
|
||||
if (inject_dir == pkt_exdata->flow_dir)
|
||||
if (type == pkt_exdata->flow_type)
|
||||
{
|
||||
tcp_seq = uint32_add(pkt_exdata->tcp_seq, pkt_exdata->inc_seq);
|
||||
tcp_ack = pkt_exdata->tcp_ack;
|
||||
@@ -398,10 +398,10 @@ static void build_and_send_tcp_packet(struct inject_plugin_ctx *ctx, struct sess
|
||||
pkt_exdata->inc_ack += (tcp_flags & TH_FIN) ? 1 : 0; // inject RST packer after FIN packer, ack should be increased by 1
|
||||
}
|
||||
|
||||
const struct packet *origin_pkt = session_get_first_packet(sess, inject_dir);
|
||||
const struct packet *origin_pkt = session_get_first_packet(sess, type);
|
||||
if (origin_pkt == NULL)
|
||||
{
|
||||
INJTECT_PLUGIN_LOG_ERROR(ctx->logger, "build TCP packet failed, %s origin packet is NULL", inject_dir == FLOW_DIRECTION_C2S ? "C2S" : "S2C");
|
||||
INJTECT_PLUGIN_LOG_ERROR(ctx->logger, "build TCP packet failed, %s origin packet is NULL", type == FLOW_TYPE_C2S ? "C2S" : "S2C");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -443,14 +443,14 @@ static void on_sess_msg(struct session *sess, int topic_id, const void *msg, voi
|
||||
struct packet *pkt = (struct packet *)msg;
|
||||
struct inject_plugin_ctx *ctx = (struct inject_plugin_ctx *)plugin_ctx;
|
||||
struct config *config = &ctx->config;
|
||||
enum flow_direction flow_dir = session_get_current_flow_direction(sess);
|
||||
enum flow_type type = session_get_flow_type(sess);
|
||||
INJTECT_PLUGIN_LOG_INFO(ctx->logger, "handle session msg: %s (C2S received packets: %lu, S2C received packets: %lu)",
|
||||
session_get0_readable_addr(sess),
|
||||
session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED),
|
||||
session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED));
|
||||
session_get_stat(sess, FLOW_TYPE_C2S, STAT_RAW_PACKETS_RECEIVED),
|
||||
session_get_stat(sess, FLOW_TYPE_S2C, STAT_RAW_PACKETS_RECEIVED));
|
||||
|
||||
struct packet_exdata pkt_exdata;
|
||||
packet_exdata_init(pkt, flow_dir, &pkt_exdata);
|
||||
packet_exdata_init(pkt, type, &pkt_exdata);
|
||||
|
||||
if (config->family == AF_INET &&
|
||||
memcmp(&config->addr.v4, &pkt_exdata.src_addr.v4, sizeof(struct in_addr)) != 0 &&
|
||||
@@ -473,55 +473,55 @@ static void on_sess_msg(struct session *sess, int topic_id, const void *msg, voi
|
||||
return;
|
||||
}
|
||||
|
||||
if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_INJECTED_PACKETS_SUCCESS) > 0 ||
|
||||
session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_INJECTED_PACKETS_SUCCESS) > 0)
|
||||
if (session_get_stat(sess, FLOW_TYPE_C2S, STAT_INJECTED_PACKETS_SUCCESS) > 0 ||
|
||||
session_get_stat(sess, FLOW_TYPE_S2C, STAT_INJECTED_PACKETS_SUCCESS) > 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (config->direction == FLOW_DIRECTION_C2S && session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED) != config->number)
|
||||
if (config->flow_type == FLOW_TYPE_C2S && session_get_stat(sess, FLOW_TYPE_C2S, STAT_RAW_PACKETS_RECEIVED) != config->number)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (config->direction == FLOW_DIRECTION_S2C && session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED) != config->number)
|
||||
if (config->flow_type == FLOW_TYPE_S2C && session_get_stat(sess, FLOW_TYPE_S2C, STAT_RAW_PACKETS_RECEIVED) != config->number)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
switch (config->type)
|
||||
switch (config->inject_type)
|
||||
{
|
||||
case INJECT_TCP_RST:
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0);
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0);
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_C2S, TH_RST | TH_ACK, NULL, 0);
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_S2C, TH_RST | TH_ACK, NULL, 0);
|
||||
session_set_discard(sess);
|
||||
break;
|
||||
case INJECT_TCP_FIN:
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_FIN | TH_ACK, NULL, 0);
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_FIN | TH_ACK, NULL, 0);
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_C2S, TH_FIN | TH_ACK, NULL, 0);
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_S2C, TH_FIN | TH_ACK, NULL, 0);
|
||||
session_set_discard(sess);
|
||||
break;
|
||||
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");
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, buffer, strlen(buffer)); // inject payload to client
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, "World\r\n", 7); // inject payload to client
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0); // inject RST to client
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0); // inject RST to server
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_S2C, TH_ACK, buffer, strlen(buffer)); // inject payload to client
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_S2C, TH_ACK, "World\r\n", 7); // inject payload to client
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_S2C, TH_RST | TH_ACK, NULL, 0); // inject RST to client
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_C2S, TH_RST | TH_ACK, NULL, 0); // inject RST to server
|
||||
session_set_discard(sess);
|
||||
break;
|
||||
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");
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, buffer, strlen(buffer)); // inject payload to client
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, "World\r\n", 7); // inject payload to client
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_FIN | TH_ACK, NULL, 0); // inject FIN to client
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0); // inject RST to client
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_FIN | TH_ACK, NULL, 0); // inject FIN to server
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0); // inject RST to server
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_S2C, TH_ACK, buffer, strlen(buffer)); // inject payload to client
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_S2C, TH_ACK, "World\r\n", 7); // inject payload to client
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_S2C, TH_FIN | TH_ACK, NULL, 0); // inject FIN to client
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_S2C, TH_RST | TH_ACK, NULL, 0); // inject RST to client
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_C2S, TH_FIN | TH_ACK, NULL, 0); // inject FIN to server
|
||||
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_C2S, TH_RST | TH_ACK, NULL, 0); // inject RST to server
|
||||
session_set_discard(sess);
|
||||
break;
|
||||
case INJECT_UDP_PAYLOAD:
|
||||
build_and_send_udp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_C2S, "Hello Server", 12);
|
||||
build_and_send_udp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, "Hello Client", 12);
|
||||
build_and_send_udp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_C2S, "Hello Server", 12);
|
||||
build_and_send_udp_packet(ctx, sess, &pkt_exdata, FLOW_TYPE_S2C, "Hello Client", 12);
|
||||
session_set_discard(sess);
|
||||
break;
|
||||
case INJECT_CTRL_MSG:
|
||||
|
||||
Reference in New Issue
Block a user