Refactored packet API to support struct layer (using union to contain different types of encapsulation headers)

This commit is contained in:
luwenpeng
2024-06-14 19:24:27 +08:00
parent 1f78881cbb
commit de4c15f43c
47 changed files with 834 additions and 701 deletions

View File

@@ -235,7 +235,7 @@ static int build_tcp_packet(const struct packet *first, uint16_t ip_id, uint8_t
struct ip6_hdr *ip6hdr;
struct raw_layer *curr;
struct raw_layer *last;
int len = packet_get_len(first);
int len = packet_get_raw_len(first);
int layers = packet_get_layer_count(first);
if ((tcp_pld == NULL && pld_len > 0) || (tcp_pld != NULL && pld_len <= 0))
@@ -248,11 +248,11 @@ static int build_tcp_packet(const struct packet *first, uint16_t ip_id, uint8_t
return -ENOMEM;
}
memcpy(pkt_buff, packet_get_data(first), len);
memcpy(pkt_buff, packet_get_raw_data(first), len);
for (int i = layers - 1; i >= 0; i--)
{
curr = (struct raw_layer *)packet_get_raw_layer(first, i);
switch (curr->type)
switch (curr->proto)
{
case LAYER_PROTO_TCP:
trim = curr->hdr_len + curr->pld_len - sizeof(struct tcphdr) - pld_len;
@@ -274,12 +274,12 @@ static int build_tcp_packet(const struct packet *first, uint16_t ip_id, uint8_t
case LAYER_PROTO_IPV4:
iphdr = (struct ip *)(pkt_buff + curr->hdr_offset);
last = (struct raw_layer *)packet_get_raw_layer(first, i + 1);
if (last->type == LAYER_PROTO_TCP)
if (last->proto == LAYER_PROTO_TCP)
{
tcphdr = (struct tcphdr *)(pkt_buff + last->hdr_offset);
tcphdr->th_sum = checksum_v4(tcphdr, len - trim - last->hdr_offset, IPPROTO_TCP, &iphdr->ip_src, &iphdr->ip_dst);
}
if (last->type == LAYER_PROTO_UDP)
if (last->proto == LAYER_PROTO_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);
@@ -289,12 +289,12 @@ static int build_tcp_packet(const struct packet *first, uint16_t ip_id, uint8_t
case LAYER_PROTO_IPV6:
ip6hdr = (struct ip6_hdr *)(pkt_buff + curr->hdr_offset);
last = (struct raw_layer *)packet_get_raw_layer(first, i + 1);
if (last->type == LAYER_PROTO_TCP)
if (last->proto == LAYER_PROTO_TCP)
{
tcphdr = (struct tcphdr *)(pkt_buff + last->hdr_offset);
tcphdr->th_sum = checksum_v6(tcphdr, len - trim - last->hdr_offset, IPPROTO_TCP, &ip6hdr->ip6_src, &ip6hdr->ip6_dst);
}
if (last->type == LAYER_PROTO_UDP)
if (last->proto == LAYER_PROTO_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);
@@ -322,7 +322,7 @@ static int build_udp_packet(const struct packet *first, const char *udp_pld, int
struct ip6_hdr *ip6hdr;
struct raw_layer *curr;
struct raw_layer *last;
int len = packet_get_len(first);
int len = packet_get_raw_len(first);
int layers = packet_get_layer_count(first);
if ((udp_pld == NULL && pld_len > 0) || (udp_pld != NULL && pld_len <= 0))
@@ -335,11 +335,11 @@ static int build_udp_packet(const struct packet *first, const char *udp_pld, int
return -ENOMEM;
}
memcpy(pkt_buff, packet_get_data(first), len);
memcpy(pkt_buff, packet_get_raw_data(first), len);
for (int i = layers - 1; i >= 0; i--)
{
curr = (struct raw_layer *)packet_get_raw_layer(first, i);
switch (curr->type)
switch (curr->proto)
{
case LAYER_PROTO_UDP:
trim = curr->hdr_len + curr->pld_len - sizeof(struct udphdr) - pld_len;
@@ -357,7 +357,7 @@ static int build_udp_packet(const struct packet *first, const char *udp_pld, int
case LAYER_PROTO_IPV4:
iphdr = (struct ip *)(pkt_buff + curr->hdr_offset);
last = (struct raw_layer *)packet_get_raw_layer(first, i + 1);
if (last->type == LAYER_PROTO_UDP)
if (last->proto == LAYER_PROTO_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);
@@ -367,7 +367,7 @@ static int build_udp_packet(const struct packet *first, const char *udp_pld, int
case LAYER_PROTO_IPV6:
ip6hdr = (struct ip6_hdr *)(pkt_buff + curr->hdr_offset);
last = (struct raw_layer *)packet_get_raw_layer(first, i + 1);
if (last->type == LAYER_PROTO_UDP)
if (last->proto == LAYER_PROTO_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);
@@ -435,15 +435,12 @@ static int inject_tcp_packet(struct stellar *st, const struct session *sess, enu
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;
memset(&inj_pkt, 0, sizeof(inj_pkt));
packet_parse(&inj_pkt, buff, pkt_len);
packet_set_origin(&inj_pkt, PACKET_ORIGIN_USERSTACK);
packet_set_origin_ctx(&inj_pkt, &meta);
packet_set_session_id(&inj_pkt, session_get_id(sess));
packet_set_sids(&inj_pkt, session_get_sids(sess, inject_dir));
packet_set_route_ctx(&inj_pkt, session_get_route_ctx(sess, inject_dir));
session_manager_record_duplicated_packet(sess_mgr, &inj_pkt, time_ms);
if (packet_io_inject(packet_io, thr_idx, &inj_pkt, 1) == 1)
{
@@ -500,15 +497,12 @@ static int inject_udp_packet(struct stellar *st, const struct session *sess, enu
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;
memset(&inj_pkt, 0, sizeof(inj_pkt));
packet_parse(&inj_pkt, buff, pkt_len);
packet_set_origin(&inj_pkt, PACKET_ORIGIN_USERSTACK);
packet_set_origin_ctx(&inj_pkt, &meta);
packet_set_session_id(&inj_pkt, session_get_id(sess));
packet_set_sids(&inj_pkt, session_get_sids(sess, inject_dir));
packet_set_route_ctx(&inj_pkt, session_get_route_ctx(sess, inject_dir));
session_manager_record_duplicated_packet(sess_mgr, &inj_pkt, time_ms);
if (packet_io_inject(packet_io, thr_idx, &inj_pkt, 1) == 1)
{
@@ -553,7 +547,7 @@ int stellar_inject_udp_payload(struct stellar *st, const struct session *sess, e
return inject_udp_packet(st, sess, inject_dir, payload, len);
}
int stellar_inject_ctrl_msg(struct stellar *st, const struct session *sess, const struct sid_list *sids, const char *msg, uint16_t len)
int stellar_inject_ctrl_msg(struct stellar *st, const struct session *sess, const struct sids *sids, const char *msg, uint16_t len)
{
// TODO
return 0;