2024-06-19 15:06:14 +08:00
|
|
|
|
#pragma once
|
2024-06-17 11:41:21 +08:00
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
extern "C"
|
|
|
|
|
|
{
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#include "layer.h"
|
|
|
|
|
|
|
|
|
|
|
|
enum tunnel_type
|
|
|
|
|
|
{
|
2024-06-19 14:43:32 +08:00
|
|
|
|
TUNNEL_IPV4 = 1, // contain layers: IPv4, (next inner layer must be IPv4 / IPv6)
|
|
|
|
|
|
TUNNEL_IPV6 = 2, // contain layers: IPv6, (next inner layer must be IPv4 / IPv6)
|
|
|
|
|
|
|
|
|
|
|
|
TUNNEL_GRE = 3, // contain layers: IPv4 + GRE
|
|
|
|
|
|
// contain layers: IPv6 + GRE
|
|
|
|
|
|
|
|
|
|
|
|
TUNNEL_GTP = 4, // contain layers: IPv4 + UDP + GTP
|
|
|
|
|
|
// contain layers: IPv6 + UDP + GTP
|
|
|
|
|
|
|
|
|
|
|
|
TUNNEL_VXLAN = 5, // contain layers: IPv4 + UDP + VXLAN
|
|
|
|
|
|
// contain layers: IPv6 + UDP + VXLAN
|
|
|
|
|
|
|
|
|
|
|
|
TUNNEL_L2TP = 6, // contain layers: IPv4 + UDP + L2TP
|
|
|
|
|
|
// contain layers: IPv6 + UDP + L2TP
|
|
|
|
|
|
|
|
|
|
|
|
TUNNEL_TEREDO = 7, // contain layers: IPv4 + UDP, (next inner layer must be IPv6)
|
2024-06-17 11:41:21 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_LAYERS_PER_TUNNEL 3
|
|
|
|
|
|
struct tunnel
|
|
|
|
|
|
{
|
|
|
|
|
|
enum tunnel_type type;
|
|
|
|
|
|
|
2024-06-19 14:43:32 +08:00
|
|
|
|
int layer_count;
|
2024-06-17 11:41:21 +08:00
|
|
|
|
struct layer layers[MAX_LAYERS_PER_TUNNEL];
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int packet_get_tunnel_count(const struct packet *pkt);
|
|
|
|
|
|
// return 0: success
|
|
|
|
|
|
// return -1: failed
|
2024-06-19 14:43:32 +08:00
|
|
|
|
int packet_get_tunnel_by_idx(const struct packet *pkt, int idx, struct tunnel *out);
|
2024-06-17 11:41:21 +08:00
|
|
|
|
|
|
|
|
|
|
#define PACKET_FOREACH_TUNNEL_INORDER(pkt, tunnel) \
|
2024-06-19 14:43:32 +08:00
|
|
|
|
for (int i = 0; i < packet_get_tunnel_count(pkt) && packet_get_tunnel_by_idx(pkt, i, &tunnel) == 0; i++)
|
2024-06-17 11:41:21 +08:00
|
|
|
|
|
|
|
|
|
|
#define PACKET_FOREACH_TUNNEL_REVERSE(pkt, tunnel) \
|
2024-06-19 14:43:32 +08:00
|
|
|
|
for (int i = packet_get_tunnel_count(pkt) - 1; i >= 0 && packet_get_tunnel_by_idx(pkt, i, &tunnel) == 0; i--)
|
2024-06-17 11:41:21 +08:00
|
|
|
|
|
2024-07-01 14:17:07 +08:00
|
|
|
|
#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; \
|
|
|
|
|
|
})
|
2024-06-17 11:41:21 +08:00
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|