2024-06-17 11:41:21 +08:00
|
|
|
|
#ifndef _TUNNEL_H
|
|
|
|
|
|
#define _TUNNEL_H
|
|
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
|
#define PACKET_GETALL_TUNNELS(pkt, tunnels) \
|
|
|
|
|
|
{ \
|
|
|
|
|
|
int num = MIN(packet_get_tunnel_count(pkt), (sizeof(tunnels) / sizeof(tunnels[0]))); \
|
2024-06-19 14:43:32 +08:00
|
|
|
|
for (int i = 0; i < num && packet_get_tunnel_by_idx(pkt, i, &tunnels[i]) == 0; i++) \
|
2024-06-17 11:41:21 +08:00
|
|
|
|
/* void */; \
|
|
|
|
|
|
return num; \
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|