feature: support crafting L3 packets with L3 payload

This commit is contained in:
luwenpeng
2024-08-12 10:49:53 +08:00
parent 12975e2da7
commit 8054b9c032
7 changed files with 306 additions and 219 deletions

View File

@@ -51,20 +51,6 @@ static inline void calc_packet_fingerprint(struct fingerprint *finger)
}
}
static void update_tcp_hdr(struct tcphdr *tcp, uint32_t seq, uint32_t ack, uint16_t win, uint8_t flags, uint16_t opts_len)
{
tcp_hdr_set_seq(tcp, seq);
tcp_hdr_set_ack(tcp, ack);
tcp_hdr_set_hdr_len(tcp, sizeof(struct tcphdr) + opts_len);
tcp_hdr_set_flags(tcp, flags);
if (win)
{
tcp_hdr_set_window(tcp, win);
}
tcp_hdr_set_urg_ptr(tcp, 0);
tcp_hdr_set_checksum(tcp, 0);
}
static void update_udp_hdr(struct udphdr *udp, int trim_len)
{
uint16_t total = udp_hdr_get_total_len(udp);
@@ -121,9 +107,36 @@ static void update_gre1_hdr(struct gre1_hdr *gre, int trim_len)
gre1_hdr_set_payload_length(gre, payload_len - trim_len);
}
static void update_packet_hdr(const struct packet *origin_pkt,
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)
// L2 -- data link layer
// LAYER_PROTO_ETHER: // SKIP
// LAYER_PROTO_PWETH: // SKIP
// LAYER_PROTO_PPP: // SKIP
// LAYER_PROTO_L2TP: // TODO ???
// L2 -- tunnel
// LAYER_PROTO_VLAN: // SKIP
// LAYER_PROTO_PPPOE: // TODO ????
// LAYER_PROTO_MPLS: // SKIP
// L3 -- network layer
// LAYER_PROTO_IPV4: // DONE
// LAYER_PROTO_IPV6: // DONE
// LAYER_PROTO_IPAH: // TODO ????
// L3 -- tunnel
// LAYER_PROTO_GRE: // DONE
// L4 -- transport layer
// LAYER_PROTO_UDP: // DONE
// LAYER_PROTO_TCP: // DONE
// LAYER_PROTO_ICMP:
// LAYER_PROTO_ICMP6:
// L4 -- tunnel
// LAYER_PROTO_VXLAN: // SKIP
// LAYER_PROTO_GTP_U: // DONE
// LAYER_PROTO_GTP_C:
static void calculate_length_and_checksum(const struct packet *origin_pkt, int layer_count, char *new_pkt_data, uint16_t new_pkt_len, int trim_len)
{
uint8_t version = 0;
uint16_t sum = 0;
@@ -141,8 +154,7 @@ static void update_packet_hdr(const struct packet *origin_pkt,
struct raw_layer *last_layer = NULL;
struct fingerprint finger = {0};
calc_packet_fingerprint(&finger);
int count = packet_get_layer_count(origin_pkt);
for (int i = count - 1; i >= 0; i--)
for (int i = layer_count - 1; i >= 0; 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);
@@ -152,7 +164,11 @@ static void update_packet_hdr(const struct packet *origin_pkt,
{
case LAYER_PROTO_TCP:
tcp = (struct tcphdr *)curr_hdr_ptr;
update_tcp_hdr(tcp, tcp_seq, tcp_ack, finger.tcp_win, tcp_flags, tcp_opts_len);
if (finger.tcp_win)
{
tcp_hdr_set_window(tcp, finger.tcp_win);
}
tcp_hdr_set_checksum(tcp, 0);
break;
case LAYER_PROTO_UDP:
udp = (struct udphdr *)curr_hdr_ptr;
@@ -253,8 +269,8 @@ struct packet *craft_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_se
}
// check the innermost layer of the original packet
int layers = packet_get_layer_count(origin_pkt);
const struct raw_layer *tcp_layer = packet_get_raw_layer(origin_pkt, layers - 1);
int layer_count = packet_get_layer_count(origin_pkt);
const struct raw_layer *tcp_layer = packet_get_raw_layer(origin_pkt, layer_count - 1);
if (tcp_layer == NULL || tcp_layer->proto != LAYER_PROTO_TCP)
{
PACKET_CRAFT_LOG_ERROR("craft TCP packet failed, the innermost layer of the original packet is not TCP");
@@ -279,9 +295,13 @@ struct packet *craft_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_se
memcpy(new_pkt_data + tcp_layer->hdr_offset + sizeof(struct tcphdr), tcp_options, tcp_options_len);
}
memcpy(new_pkt_data + tcp_layer->hdr_offset + sizeof(struct tcphdr) + tcp_options_len, tcp_payload, tcp_payload_len);
struct tcphdr *hdr = (struct tcphdr *)(new_pkt_data + tcp_layer->hdr_offset);
tcp_hdr_set_seq(hdr, tcp_seq);
tcp_hdr_set_ack(hdr, tcp_ack);
tcp_hdr_set_flags(hdr, tcp_flags);
tcp_hdr_set_hdr_len(hdr, sizeof(struct tcphdr) + tcp_options_len);
// update the headers of the new packet
update_packet_hdr(origin_pkt, new_pkt_data, new_pkt_len, trim_len, tcp_seq, tcp_ack, tcp_flags, tcp_options_len);
calculate_length_and_checksum(origin_pkt, layer_count, new_pkt_data, new_pkt_len, trim_len);
packet_parse(new_pkt, new_pkt_data, new_pkt_len);
memcpy(&new_pkt->meta, &origin_pkt->meta, sizeof(struct metadata));
@@ -300,8 +320,8 @@ struct packet *craft_udp_packet(const struct packet *origin_pkt, const char *udp
}
// check the innermost layer of the original packet
int layers = packet_get_layer_count(origin_pkt);
const struct raw_layer *udp_layer = packet_get_raw_layer(origin_pkt, layers - 1);
int layer_count = packet_get_layer_count(origin_pkt);
const struct raw_layer *udp_layer = packet_get_raw_layer(origin_pkt, layer_count - 1);
if (udp_layer == NULL || udp_layer->proto != LAYER_PROTO_UDP)
{
PACKET_CRAFT_LOG_ERROR("craft UDP packet failed, the innermost layer of the original packet is not UDP");
@@ -323,8 +343,7 @@ struct packet *craft_udp_packet(const struct packet *origin_pkt, const char *udp
memcpy(new_pkt_data, packet_get_raw_data(origin_pkt), udp_layer->hdr_offset + sizeof(struct udphdr));
memcpy(new_pkt_data + udp_layer->hdr_offset + sizeof(struct udphdr), udp_payload, udp_payload_len);
// update the headers of the new packet
update_packet_hdr(origin_pkt, new_pkt_data, new_pkt_len, trim_len, 0, 0, 0, 0);
calculate_length_and_checksum(origin_pkt, layer_count, new_pkt_data, new_pkt_len, trim_len);
packet_parse(new_pkt, new_pkt_data, new_pkt_len);
memcpy(&new_pkt->meta, &origin_pkt->meta, sizeof(struct metadata));
@@ -333,143 +352,71 @@ struct packet *craft_udp_packet(const struct packet *origin_pkt, const char *udp
return new_pkt;
}
struct packet *craft_packet_from_scratch(const struct layer layers[], uint16_t layer_count, const char *payload, uint16_t payload_len)
struct packet *craft_l3_packet(const struct packet *origin_pkt, uint8_t ip_proto, const char *l3_payload, uint16_t l3_payload_len)
{
// check arguments
if (layers == NULL || layer_count == 0 || (payload == NULL && payload_len != 0) || (payload != NULL && payload_len == 0))
if (origin_pkt == NULL || (l3_payload == NULL && l3_payload_len != 0) || (l3_payload != NULL && l3_payload_len == 0))
{
PACKET_CRAFT_LOG_ERROR("craft packet from scratch failed, invalid arguments");
PACKET_CRAFT_LOG_ERROR("craft L3 packet failed, invalid arguments");
return NULL;
}
int i = 0;
int layers = packet_get_layer_count(origin_pkt);
const struct raw_layer *l3_layer = NULL;
for (i = layers - 1; i >= 0; i--)
{
l3_layer = packet_get_raw_layer(origin_pkt, i);
if (l3_layer->proto == LAYER_PROTO_IPV4 || l3_layer->proto == LAYER_PROTO_IPV6)
{
break;
}
else
{
l3_layer = NULL;
}
}
if (l3_layer == NULL)
{
PACKET_CRAFT_LOG_ERROR("craft L3 packet failed, the original packet does not contain an IP layer");
return NULL;
}
// calculate the new packet length
uint16_t new_pkt_len = 0;
for (int i = 0; i < layer_count; i++)
{
if (layers[i].hdr.raw == NULL || layers[i].hdr_len == 0)
{
PACKET_CRAFT_LOG_ERROR("craft packet from scratch failed, the header of layer %d is invalid", i);
return NULL;
}
new_pkt_len += layers[i].hdr_len;
}
new_pkt_len += payload_len;
// trim IPv4 options
// trim IPv6 extension headers
int l3_hdr_len = l3_layer->proto == LAYER_PROTO_IPV4 ? sizeof(struct ip) : sizeof(struct ip6_hdr);
int trim_len = l3_layer->hdr_len + l3_layer->pld_len - l3_payload_len - l3_hdr_len;
uint16_t new_pkt_len = origin_pkt->data_len - origin_pkt->trim_len - trim_len;
struct packet *new_pkt = packet_new(new_pkt_len);
if (new_pkt == NULL)
{
PACKET_CRAFT_LOG_ERROR("craft packet from scratch failed, no space to allocate new packet");
PACKET_CRAFT_LOG_ERROR("craft L3 packet failed, no space to allocate new packet");
return NULL;
}
// copy the data to the new packet
char *new_pkt_data = (char *)packet_get_raw_data(new_pkt);
int offset = 0;
for (int i = 0; i < layer_count; i++)
memcpy(new_pkt_data, packet_get_raw_data(origin_pkt), l3_layer->hdr_offset + l3_hdr_len);
if (l3_payload)
{
memcpy(new_pkt_data + offset, layers[i].hdr.raw, layers[i].hdr_len);
offset += layers[i].hdr_len;
memcpy(new_pkt_data + l3_layer->hdr_offset + l3_hdr_len, l3_payload, l3_payload_len);
}
memcpy(new_pkt_data + offset, payload, payload_len);
// update the headers of the new packet
uint8_t version = 0;
uint16_t curr_hdr_len = 0;
char *curr_hdr_ptr = NULL;
struct tcphdr *tcp = NULL;
struct udphdr *udp = NULL;
struct ip *ip4 = NULL;
struct ip6_hdr *ip6 = NULL;
struct gtp1_hdr *gtp1 = NULL;
struct gtp2_hdr *gtp2 = NULL;
// update checksums and lengths
uint16_t curr_payload_len = payload_len;
for (int i = layer_count - 1; i >= 0; i--)
// update ip_proto
if (l3_layer->proto == LAYER_PROTO_IPV4)
{
curr_hdr_len = layers[i].hdr_len;
curr_hdr_ptr = new_pkt_data + new_pkt_len - curr_hdr_len - curr_payload_len;
switch (layers[i].proto)
{
case LAYER_PROTO_TCP:
tcp = (struct tcphdr *)curr_hdr_ptr;
// update the TCP header
tcp_hdr_set_hdr_len(tcp, curr_hdr_len);
tcp_hdr_set_checksum(tcp, 0);
curr_payload_len += curr_hdr_len;
break;
case LAYER_PROTO_UDP:
udp = (struct udphdr *)curr_hdr_ptr;
// update the UDP header
udp_hdr_set_total_len(udp, curr_hdr_len + curr_payload_len);
udp_hdr_set_checksum(udp, 0);
curr_payload_len += curr_hdr_len;
break;
case LAYER_PROTO_IPV4:
ip4 = (struct ip *)curr_hdr_ptr;
// update the checksums of the upper layer
if (i + 1 < layer_count && layers[i + 1].proto == LAYER_PROTO_TCP)
{
tcp = (struct tcphdr *)(new_pkt_data + new_pkt_len - curr_payload_len);
tcp->th_sum = checksum_v4(tcp, curr_payload_len, IPPROTO_TCP, &ip4->ip_src, &ip4->ip_dst);
}
if (i + 1 < layer_count && layers[i + 1].proto == LAYER_PROTO_UDP)
{
udp = (struct udphdr *)(new_pkt_data + new_pkt_len - curr_payload_len);
udp->uh_sum = checksum_v4(udp, curr_payload_len, IPPROTO_UDP, &ip4->ip_src, &ip4->ip_dst);
}
// update the IPv4 header
ip4_hdr_set_hdr_len(ip4, curr_hdr_len);
ip4_hdr_set_total_len(ip4, curr_hdr_len + curr_payload_len);
ip4->ip_sum = 0;
ip4->ip_sum = checksum((const void *)ip4, curr_hdr_len);
curr_payload_len += curr_hdr_len;
break;
case LAYER_PROTO_IPV6:
ip6 = (struct ip6_hdr *)curr_hdr_ptr;
// update the checksums of the upper layer
if (i + 1 < layer_count && layers[i + 1].proto == LAYER_PROTO_TCP)
{
tcp = (struct tcphdr *)(new_pkt_data + new_pkt_len - curr_payload_len);
tcp->th_sum = checksum_v6(tcp, curr_payload_len, IPPROTO_TCP, &ip6->ip6_src, &ip6->ip6_dst);
}
if (i + 1 < layer_count && layers[i + 1].proto == LAYER_PROTO_UDP)
{
udp = (struct udphdr *)(new_pkt_data + new_pkt_len - curr_payload_len);
udp->uh_sum = checksum_v6(udp, curr_payload_len, IPPROTO_UDP, &ip6->ip6_src, &ip6->ip6_dst);
}
// update the IPv6 header
ip6_hdr_set_payload_len(ip6, curr_hdr_len + curr_payload_len - sizeof(struct ip6_hdr));
curr_payload_len += curr_hdr_len;
break;
case LAYER_PROTO_GTP_C: /* fall through */
case LAYER_PROTO_GTP_U:
version = peek_gtp_version(curr_hdr_ptr, curr_hdr_len);
if (version == 1)
{
gtp1 = (struct gtp1_hdr *)curr_hdr_ptr;
// update the GTP header
gtp1_hdr_set_msg_len(gtp1, curr_hdr_len + curr_payload_len - sizeof(struct gtp1_hdr));
}
if (version == 2)
{
gtp2 = (struct gtp2_hdr *)curr_hdr_ptr;
// update the GTP header
gtp2_hdr_set_msg_len(gtp2, curr_hdr_len + curr_payload_len - 4);
}
curr_payload_len += curr_hdr_len;
break;
case LAYER_PROTO_GRE:
// TODO
curr_payload_len += curr_hdr_len;
break;
default:
curr_payload_len += curr_hdr_len;
break;
}
struct ip *ip4 = (struct ip *)(new_pkt_data + l3_layer->hdr_offset);
ip4_hdr_set_protocol(ip4, ip_proto);
}
else
{
struct ip6_hdr *ip6 = (struct ip6_hdr *)(new_pkt_data + l3_layer->hdr_offset);
ip6_hdr_set_next_header(ip6, ip_proto);
}
calculate_length_and_checksum(origin_pkt, i + 1, new_pkt_data, new_pkt_len, trim_len);
packet_parse(new_pkt, new_pkt_data, new_pkt_len);
// no metadata for the new packet from scratch
memcpy(&new_pkt->meta, &origin_pkt->meta, sizeof(struct metadata));
new_pkt->meta.origin_ctx = NULL;
return new_pkt;
}
}