Add the packet_injector tool to test the packet injection effect

This commit is contained in:
luwenpeng
2024-05-11 18:58:36 +08:00
parent 7f1e7a23de
commit cda77c6f6f
17 changed files with 851 additions and 337 deletions

View File

@@ -1,5 +1,7 @@
#include <time.h>
#include <errno.h>
#include <assert.h>
#include "tcp_utils.h"
#include "udp_utils.h"
#include "ipv4_utils.h"
@@ -12,6 +14,13 @@
#define INJECT_PACKET_LOG_ERROR(format, ...) LOG_ERROR("inject packet", format, ##__VA_ARGS__)
#define INJECT_PACKE_LOG_DEBUG(format, ...) LOG_DEBUG("inject packet", format, ##__VA_ARGS__)
struct tcp_fingerprint
{
uint16_t ipid;
uint8_t ttl;
uint16_t win;
};
static uint16_t checksum(const char *data, uint16_t len)
{
uint32_t sum = 0;
@@ -155,7 +164,7 @@ static void update_ip6_hdr(struct ip6_hdr *ip6hdr, int trim)
ipv6_hdr_set_payload_len(ip6hdr, len - trim);
}
static inline void calc_tcp_seq_and_ack(const struct session *sess, enum flow_direction inject_dir, uint32_t *seq, uint32_t *ack)
static inline void calc_tcp_seq_ack(const struct session *sess, enum flow_direction inject_dir, uint32_t *seq, uint32_t *ack)
{
/*
* +--------+ current packet +---------+ C2S RST +--------+
@@ -173,6 +182,9 @@ static inline void calc_tcp_seq_and_ack(const struct session *sess, enum flow_di
* inject direction != current direction (inject S2C RST)
* seq = current_packet_ack
* ack = current_packet_seq + current_packet_payload_len
* or if current packet is a SYN-ACK packet
* seq = current_packet_seq
* ack = current_packet_ack + current_packet_payload_len + 1
*/
enum flow_direction curr_dir = session_get_flow_direction(sess);
@@ -185,36 +197,26 @@ static inline void calc_tcp_seq_and_ack(const struct session *sess, enum flow_di
else
{
*seq = tcp_curr_half->ack;
*ack = tcp_curr_half->seq + tcp_curr_half->len;
*ack = tcp_curr_half->seq + tcp_curr_half->len + (tcp_curr_half->flags & TH_SYN ? 1 : 0);
}
}
static inline void calc_ipid_ttl_win(uint16_t *ipid, uint8_t *ttl, uint16_t *win)
static inline void calc_tcp_fingerprint(struct tcp_fingerprint *finger)
{
#define RANGE(rand, start, end) (start + rand % (end - start + 1)) // [start, end]
struct timespec curtime;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &curtime);
uint64_t random = (0x013579ABCDEF ^ (uint64_t)curtime.tv_nsec);
*ipid = (uint16_t)(RANGE(random, 32767, 65535));
*ttl = (uint8_t)(RANGE(random, 48, 120));
*win = (uint16_t)(RANGE(random, 1000, 1460));
finger->ipid = (uint16_t)(RANGE(random, 32767, 65535));
finger->ttl = (uint8_t)(RANGE(random, 48, 120));
finger->win = (uint16_t)(RANGE(random, 1000, 1460));
}
/******************************************************************************
* Public API
******************************************************************************/
struct fingerprint
{
uint16_t ipid;
uint8_t ttl;
uint16_t win;
};
// return packet length
static int build_tcp_packet(const struct packet *first, const struct fingerprint *finger,
uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, char *tcp_pld, int pld_len, char *pkt_buff, int buff_size)
int build_tcp_packet(const struct packet *first, uint16_t ip_id, uint8_t ip_ttl,
uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, uint16_t tcp_win,
const char *tcp_pld, int pld_len, char *pkt_buff, int buff_size)
{
int trim = 0;
struct tcphdr *tcphdr;
@@ -244,12 +246,12 @@ static int build_tcp_packet(const struct packet *first, const struct fingerprint
{
case LAYER_TYPE_TCP:
trim = curr->hdr_len + curr->pld_len - sizeof(struct tcphdr) + pld_len;
if (len + trim > buff_size)
if (len - trim > buff_size)
{
return -ENOMEM;
}
tcphdr = (struct tcphdr *)(pkt_buff + curr->hdr_offset);
update_tcp_hdr(tcphdr, tcp_seq, tcp_ack, finger->win, TH_RST | TH_ACK);
update_tcp_hdr(tcphdr, tcp_seq, tcp_ack, tcp_win, tcp_flags);
if (pld_len)
{
memcpy(pkt_buff + curr->hdr_offset + sizeof(struct tcphdr), tcp_pld, pld_len);
@@ -270,9 +272,9 @@ static int build_tcp_packet(const struct packet *first, const struct fingerprint
if (last->type == LAYER_TYPE_UDP)
{
udphdr = (struct udphdr *)(pkt_buff + last->hdr_offset);
udphdr->uh_sum = checksum_v4(udphdr, len - trim - last->hdr_offset + pld_len, IPPROTO_UDP, &iphdr->ip_src, &iphdr->ip_dst);
udphdr->uh_sum = checksum_v4(udphdr, len - trim - last->hdr_offset, IPPROTO_UDP, &iphdr->ip_src, &iphdr->ip_dst);
}
update_ip4_hdr(iphdr, finger->ipid, finger->ttl, trim);
update_ip4_hdr(iphdr, ip_id, ip_ttl, trim);
break;
case LAYER_TYPE_IPV6:
ip6hdr = (struct ip6_hdr *)(pkt_buff + curr->hdr_offset);
@@ -285,7 +287,7 @@ static int build_tcp_packet(const struct packet *first, const struct fingerprint
if (last->type == LAYER_TYPE_UDP)
{
udphdr = (struct udphdr *)(pkt_buff + last->hdr_offset);
udphdr->uh_sum = checksum_v6(udphdr, len - trim - last->hdr_offset + pld_len, IPPROTO_UDP, &ip6hdr->ip6_src, &ip6hdr->ip6_dst);
udphdr->uh_sum = checksum_v6(udphdr, len - trim - last->hdr_offset, IPPROTO_UDP, &ip6hdr->ip6_src, &ip6hdr->ip6_dst);
}
update_ip6_hdr(ip6hdr, trim);
break;
@@ -301,39 +303,120 @@ static int build_tcp_packet(const struct packet *first, const struct fingerprint
return len - trim;
}
// return 0: success, -1: failed
int stellar_inject_tcp_flags(const struct session *sess, enum flow_direction inject_dir, uint8_t flags)
// return packet length
int build_udp_packet(const struct packet *first, const char *udp_pld, int pld_len, char *pkt_buff, int buff_size)
{
int trim = 0;
struct udphdr *udphdr;
struct ip *iphdr;
struct ip6_hdr *ip6hdr;
struct packet_layer *curr;
struct packet_layer *last;
int len = packet_get_len(first);
int8_t layers = packet_get_layers_number(first);
if ((udp_pld == NULL && pld_len > 0) || (udp_pld != NULL && pld_len <= 0))
{
return -EINVAL;
}
if (len > buff_size)
{
return -ENOMEM;
}
memcpy(pkt_buff, packet_get_data(first), len);
for (int8_t i = layers - 1; i >= 0; i--)
{
curr = (struct packet_layer *)packet_get_layer(first, i);
switch (curr->type)
{
case LAYER_TYPE_UDP:
trim = curr->hdr_len + curr->pld_len - sizeof(struct udphdr) + pld_len;
if (len - trim > buff_size)
{
return -ENOMEM;
}
udphdr = (struct udphdr *)(pkt_buff + curr->hdr_offset);
update_udp_hdr(udphdr, trim);
if (pld_len)
{
memcpy(pkt_buff + curr->hdr_offset + sizeof(struct udphdr), udp_pld, pld_len);
}
break;
case LAYER_TYPE_IPV4:
iphdr = (struct ip *)(pkt_buff + curr->hdr_offset);
last = (struct packet_layer *)packet_get_layer(first, i + 1);
if (last->type == LAYER_TYPE_UDP)
{
udphdr = (struct udphdr *)(pkt_buff + last->hdr_offset);
udphdr->uh_sum = checksum_v4(udphdr, len - trim - last->hdr_offset, IPPROTO_UDP, &iphdr->ip_src, &iphdr->ip_dst);
}
update_ip4_hdr(iphdr, 0, 0, trim);
break;
case LAYER_TYPE_IPV6:
ip6hdr = (struct ip6_hdr *)(pkt_buff + curr->hdr_offset);
last = (struct packet_layer *)packet_get_layer(first, i + 1);
if (last->type == LAYER_TYPE_UDP)
{
udphdr = (struct udphdr *)(pkt_buff + last->hdr_offset);
udphdr->uh_sum = checksum_v6(udphdr, len - trim - last->hdr_offset, IPPROTO_UDP, &ip6hdr->ip6_src, &ip6hdr->ip6_dst);
}
update_ip6_hdr(ip6hdr, trim);
break;
case LAYER_TYPE_GRE:
return -EPROTONOSUPPORT;
// TODO
break;
default:
break;
}
}
return len - trim;
}
int inject_tcp_packet(const struct session *sess, enum flow_direction inject_dir, uint8_t tcp_flags, const char *payload, uint16_t len)
{
#define TCP_FLAGS_LOG_FORMAT "URG:%d, ACK:%d, PSH:%d, RST:%d, SYN:%d, FIN:%d"
#define TCP_FLAGS_LOG_VALUE(flags) \
(((flags) & TH_URG) ? 1 : 0), (((flags) & TH_ACK) ? 1 : 0), \
(((flags) & TH_PUSH) ? 1 : 0), (((flags) & TH_RST) ? 1 : 0), \
(((flags) & TH_SYN) ? 1 : 0), (((flags) & TH_FIN) ? 1 : 0)
uint16_t thr_idx = stellar_get_current_thread_index();
if (session_get_type(sess) != SESSION_TYPE_TCP)
{
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
INJECT_PACKET_LOG_ERROR("session %ld is not a TCP session, cannot inject TCP RST", session_get_id(sess));
return -1;
INJECT_PACKET_LOG_ERROR("session %ld is not a TCP session, cannot inject TCP packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d)",
session_get_id(sess), TCP_FLAGS_LOG_VALUE(tcp_flags), len);
return 0;
}
const struct packet *pkt = session_get_first_packet(sess, inject_dir);
if (pkt == NULL)
{
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
INJECT_PACKET_LOG_ERROR("session %ld has no %s first packet, cannot inject TCP RST", session_get_id(sess), flow_direction_to_str(inject_dir));
return -1;
INJECT_PACKET_LOG_ERROR("session %ld has no %s first packet, cannot inject TCP packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d)",
session_get_id(sess), flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), len);
return 0;
}
struct fingerprint finger = {0};
struct tcp_fingerprint finger = {0};
uint32_t tcp_seq = 0;
uint32_t tcp_ack = 0;
char buff[4096] = {0};
calc_tcp_seq_and_ack(sess, inject_dir, &tcp_seq, &tcp_ack);
calc_ipid_ttl_win(&finger.ipid, &finger.ttl, &finger.win);
calc_tcp_seq_ack(sess, inject_dir, &tcp_seq, &tcp_ack);
calc_tcp_fingerprint(&finger);
int len = build_tcp_packet(pkt, &finger, tcp_seq, tcp_ack, flags, NULL, 0, buff, sizeof(buff));
if (len <= 0)
int pkt_len = build_tcp_packet(pkt, finger.ipid, finger.ttl, tcp_seq, tcp_ack, tcp_flags, finger.win, payload, len, buff, sizeof(buff));
if (pkt_len <= 0)
{
INJECT_PACKET_LOG_ERROR("session %ld build TCP %s RST packet failed, %s", session_get_id(sess), flow_direction_to_str(inject_dir), strerror(len));
INJECT_PACKET_LOG_ERROR("session %ld build TCP %s packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d) failed, %s",
session_get_id(sess), flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), len, strerror(len));
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
return -1;
return 0;
}
struct inject_packet_meta meta = {0};
@@ -342,31 +425,112 @@ int stellar_inject_tcp_flags(const struct session *sess, enum flow_direction inj
session_get_sid_list(sess, inject_dir, &meta.sids);
struct packet inj_pkt;
packet_parse(&inj_pkt, buff, len);
packet_parse(&inj_pkt, buff, pkt_len);
packet_set_origin(&inj_pkt, PACKET_ORIGIN_USERSTACK);
packet_set_origin_ctx(&inj_pkt, &meta);
if (packet_io_inject(runtime->packet_io, thr_idx, &inj_pkt, 1) == 1)
{
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_SUCCESS, 1);
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_BYTES_SUCCESS, len);
return 0;
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_BYTES_SUCCESS, pkt_len);
INJECT_PACKE_LOG_DEBUG("session %ld inject TCP %s packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d) success",
session_get_id(sess), flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), len);
return pkt_len;
}
else
{
INJECT_PACKET_LOG_ERROR("session %ld inject TCP %s RST packet failed, packet I/O nospace", session_get_id(sess), flow_direction_to_str(inject_dir));
INJECT_PACKET_LOG_ERROR("session %ld inject TCP %s packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d) failed, packet I/O nospace",
session_get_id(sess), flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), len);
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
return -1;
return 0;
}
}
int inject_udp_packet(const struct session *sess, enum flow_direction inject_dir, const char *payload, uint16_t len)
{
uint16_t thr_idx = stellar_get_current_thread_index();
if (session_get_type(sess) != SESSION_TYPE_UDP)
{
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
INJECT_PACKET_LOG_ERROR("session %ld is not a UDP session, cannot inject UDP packet (payload len:%d)",
session_get_id(sess), len);
return 0;
}
const struct packet *pkt = session_get_first_packet(sess, inject_dir);
if (pkt == NULL)
{
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
INJECT_PACKET_LOG_ERROR("session %ld has no %s first packet, cannot inject UDP packet (payload len:%d)",
session_get_id(sess), flow_direction_to_str(inject_dir), len);
return 0;
}
char buff[4096] = {0};
int pkt_len = build_udp_packet(pkt, payload, len, buff, sizeof(buff));
if (pkt_len <= 0)
{
INJECT_PACKET_LOG_ERROR("session %ld build UDP %s packet (payload len:%d) failed, %s",
session_get_id(sess), flow_direction_to_str(inject_dir), len, strerror(len));
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
return 0;
}
struct inject_packet_meta meta = {0};
meta.session_id = session_get_id(sess);
session_get_route_ctx(sess, inject_dir, &meta.route);
session_get_sid_list(sess, inject_dir, &meta.sids);
struct packet inj_pkt;
packet_parse(&inj_pkt, buff, pkt_len);
packet_set_origin(&inj_pkt, PACKET_ORIGIN_USERSTACK);
packet_set_origin_ctx(&inj_pkt, &meta);
if (packet_io_inject(runtime->packet_io, thr_idx, &inj_pkt, 1) == 1)
{
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_SUCCESS, 1);
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_BYTES_SUCCESS, pkt_len);
INJECT_PACKE_LOG_DEBUG("session %ld inject UDP %s packet (payload len:%d) success",
session_get_id(sess), flow_direction_to_str(inject_dir), len);
return pkt_len;
}
else
{
INJECT_PACKET_LOG_ERROR("session %ld inject UDP %s packet (payload len:%d) failed, packet I/O nospace",
session_get_id(sess), flow_direction_to_str(inject_dir), len);
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
return 0;
}
}
/******************************************************************************
* Public API
******************************************************************************/
int stellar_inject_tcp_rst(const struct session *sess, enum flow_direction inject_dir)
{
return inject_tcp_packet(sess, inject_dir, TH_RST | TH_ACK, NULL, 0);
}
int stellar_inject_tcp_fin(const struct session *sess, enum flow_direction inject_dir)
{
return inject_tcp_packet(sess, inject_dir, TH_FIN | TH_ACK, NULL, 0);
}
int stellar_inject_payload(const struct session *sess, enum flow_direction inject_dir, const char *payload, uint16_t len)
{
// TODO
return -1;
switch (session_get_type(sess))
{
case SESSION_TYPE_TCP:
return inject_tcp_packet(sess, inject_dir, TH_ACK, payload, len);
case SESSION_TYPE_UDP:
return inject_udp_packet(sess, inject_dir, payload, len);
default:
return 0;
}
}
int stellar_inject_ctrl_msg(const struct session *sess, const struct sid_list *sids, const char *msg, uint16_t len)
{
// TODO
return -1;
}
return 0;
}