TSG-16531 PacketAdapter适配容器环境,使用mrzcpd收包,通过RAW Socket注RST包

This commit is contained in:
luwenpeng
2023-08-09 18:47:16 +08:00
parent 1063574ca0
commit e34aa3f5e2
65 changed files with 4379 additions and 1174 deletions

View File

@@ -1,36 +0,0 @@
#ifndef _DECODE_GTP_H
#define _DECODE_GTP_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include "public.h"
/* According to 3GPP TS 29.060. */
typedef struct gtp1_header_s
{
uint8_t flags;
uint8_t type;
uint16_t length;
uint32_t tid;
} __attribute__((packed)) gtp1_header_t;
typedef struct gtp_info_s
{
gtp1_header_t *hdr;
uint8_t *payload;
uint32_t hdr_len;
uint32_t payload_len;
} gtp_info_t;
int decode_gtp(gtp_info_t *packet, const uint8_t *data, uint32_t len);
int dump_gtp_info(gtp_info_t *packet, char *buff, size_t size);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -1,55 +0,0 @@
#ifndef _DECODE_IPV4_H
#define _DECODE_IPV4_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include "public.h"
#define IPV4_HEADER_LEN 20
typedef struct ipv4_header_s
{
uint8_t ip_verhl; // version & header length
uint8_t ip_tos;
uint16_t ip_len;
uint16_t ip_id;
uint16_t ip_off;
uint8_t ip_ttl;
uint8_t ip_proto;
uint16_t ip_csum;
union
{
struct
{
struct in_addr ip_src;
struct in_addr ip_dst;
} ip4_un1;
uint16_t ip_addrs[4];
} ip4_hdrun1;
} __attribute__((__packed__)) ipv4_header_t;
typedef struct ipv4_info_s
{
char src_addr[INET_ADDRSTRLEN];
char dst_addr[INET_ADDRSTRLEN];
ipv4_header_t *hdr;
uint8_t *payload;
uint8_t next_protocol;
uint32_t hdr_len;
uint32_t opts_len;
uint32_t payload_len;
} ipv4_info_t;
int decode_ipv4(ipv4_info_t *packet, const uint8_t *data, uint32_t len);
int dump_ipv4_info(ipv4_info_t *packet, char *buff, size_t size);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -1,58 +0,0 @@
#ifndef _DECODE_IPV6_H
#define _DECODE_IPV6_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include "public.h"
#define IPV6_HEADER_LEN 40
typedef struct ipv6_header_s
{
union
{
struct ip6_un1_
{
uint32_t ip6_un1_flow; /* 20 bits of flow-ID */
uint16_t ip6_un1_plen; /* payload length */
uint8_t ip6_un1_nxt; /* next header */
uint8_t ip6_un1_hlim; /* hop limit */
} ip6_un1;
uint8_t ip6_un2_vfc; /* 4 bits version, top 4 bits class */
} ip6_hdrun;
union
{
struct
{
uint32_t ip6_src[4];
uint32_t ip6_dst[4];
} ip6_un2;
uint16_t ip6_addrs[16];
} ip6_hdrun2;
} __attribute__((__packed__)) ipv6_header_t;
typedef struct ipv6_info_s
{
char src_addr[INET6_ADDRSTRLEN];
char dst_addr[INET6_ADDRSTRLEN];
ipv6_header_t *hdr;
uint8_t *payload;
uint8_t next_protocol;
uint32_t hdr_len;
uint32_t payload_len;
} ipv6_info_t;
int decode_ipv6(ipv6_info_t *packet, const uint8_t *data, uint32_t len);
int dump_ipv6_info(ipv6_info_t *packet, char *buff, size_t size);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -1,46 +0,0 @@
#ifndef _DECODE_TCP_H
#define _DECODE_TCP_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include "public.h"
#define TCP_HEADER_LEN 20
typedef struct tcp_header_s
{
uint16_t th_sport; /**< source port */
uint16_t th_dport; /**< destination port */
uint32_t th_seq; /**< sequence number */
uint32_t th_ack; /**< acknowledgement number */
uint8_t th_offx2; /**< offset and reserved */
uint8_t th_flags; /**< pkt flags */
uint16_t th_win; /**< pkt window */
uint16_t th_sum; /**< checksum */
uint16_t th_urp; /**< urgent pointer */
} __attribute__((__packed__)) tcp_header_t;
typedef struct tcp_info_s
{
uint16_t src_port;
uint16_t dst_port;
tcp_header_t *hdr;
uint8_t *payload;
uint32_t opt_len;
uint32_t hdr_len;
uint32_t payload_len;
} tcp_info_t;
int decode_tcp(tcp_info_t *packet, const uint8_t *data, uint32_t len);
int dump_tcp_info(tcp_info_t *packet, char *buff, size_t size);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -1,40 +0,0 @@
#ifndef _DECODE_UDP_H
#define _DECODE_UDP_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include "public.h"
#define UDP_HEADER_LEN 8
typedef struct udp_header_s
{
uint16_t udp_src_port;
uint16_t udp_dst_port;
uint16_t udp_len;
uint16_t udp_sum;
} __attribute__((__packed__)) udp_header_t;
typedef struct udp_info_s
{
uint16_t src_port;
uint16_t dst_port;
udp_header_t *hdr;
uint8_t *payload;
uint32_t hdr_len;
uint32_t payload_len;
} udp_info_t;
int decode_udp(udp_info_t *packet, const uint8_t *data, uint32_t len);
int dump_udp_info(udp_info_t *packet, char *buff, size_t size);
#ifdef __cpluscplus
}
#endif
#endif

73
common/include/log.h Normal file
View File

@@ -0,0 +1,73 @@
#ifndef _LOG_H
#define _LOG_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <stdio.h>
#include <MESA/MESA_handle_logger.h>
extern void *default_logger;
int LOG_INIT(const char *profile);
void LOG_CLOSE(void);
void LOG_RELOAD(void);
#define LOG_DEBUG(format, ...) \
do \
{ \
if (default_logger == NULL) \
{ \
fprintf(stdout, "[DEBUG] " format "\n", ##__VA_ARGS__); \
fflush(stdout); \
} \
else \
{ \
if (MESA_handle_runtime_log_level_enabled(default_logger, RLOG_LV_DEBUG)) \
{ \
MESA_handle_runtime_log(default_logger, RLOG_LV_DEBUG, __FUNCTION__, format, ##__VA_ARGS__); \
} \
} \
} while (0)
#define LOG_INFO(format, ...) \
do \
{ \
if (default_logger == NULL) \
{ \
fprintf(stdout, "[INFOR] " format "\n", ##__VA_ARGS__); \
fflush(stdout); \
} \
else \
{ \
if (MESA_handle_runtime_log_level_enabled(default_logger, RLOG_LV_INFO)) \
{ \
MESA_handle_runtime_log(default_logger, RLOG_LV_INFO, __FUNCTION__, format, ##__VA_ARGS__); \
} \
} \
} while (0)
#define LOG_ERROR(format, ...) \
do \
{ \
if (default_logger == NULL) \
{ \
fprintf(stdout, "[ERROR] " format "\n", ##__VA_ARGS__); \
fflush(stdout); \
} \
else \
{ \
if (MESA_handle_runtime_log_level_enabled(default_logger, RLOG_LV_FATAL)) \
{ \
MESA_handle_runtime_log(default_logger, RLOG_LV_FATAL, __FUNCTION__, format, ##__VA_ARGS__); \
} \
} \
} while (0)
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -0,0 +1,19 @@
#ifndef _PACKET_INJECT_H
#define _PACKET_INJECT_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <netinet/ip.h>
#include <netinet/ip6.h>
int packet_inject_ipv4(struct in_addr *ip_dst, const char *data, int len);
int packet_inject_ipv6(struct in6_addr *ip6_dst, const char *data, int len);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -0,0 +1,35 @@
#ifndef _PACKET_IO_H
#define _PACKET_IO_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <marsio.h>
#define MAX_THREAD_NUM 128
enum action
{
ACTION_BYPASS = 0x1,
ACTION_DROP = 0x2,
};
struct packet_io;
typedef enum action packet_handle_cb(const char *data, int len, void *args);
void packet_io_set_callback(struct packet_io *handle, packet_handle_cb *cb, void *args);
struct packet_io *packet_io_create(const char *profile);
void packet_io_destory(struct packet_io *handle);
int packet_io_thread_init(struct packet_io *handle, int thread_index);
void packet_io_thread_wait(struct packet_io *handle, int thread_index, int timeout_ms);
int packet_io_thread_polling(struct packet_io *handle, int thread_index);
int packet_io_thread_number(struct packet_io *handle);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -0,0 +1,85 @@
#ifndef _PACKET_PARSER_H
#define _PACKET_PARSER_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <stdint.h>
enum layer_type
{
// 数据链路层
LAYER_TYPE_ETHER = 1 << 0,
LAYER_TYPE_PPP = 1 << 1,
LAYER_TYPE_HDLC = 1 << 2,
LAYER_TYPE_L2 = (LAYER_TYPE_ETHER | LAYER_TYPE_PPP | LAYER_TYPE_HDLC),
// 数据链路层 -- 隧道
LAYER_TYPE_VLAN = 1 << 3,
LAYER_TYPE_PPPOE = 1 << 4,
LAYER_TYPE_MPLS = 1 << 5,
LAYER_TYPE_L2_TUN = (LAYER_TYPE_VLAN | LAYER_TYPE_PPPOE | LAYER_TYPE_MPLS),
// 网络层
LAYER_TYPE_IPV4 = 1 << 6,
LAYER_TYPE_IPV6 = 1 << 7,
LAYER_TYPE_L3 = (LAYER_TYPE_IPV4 | LAYER_TYPE_IPV6),
// 网络层 -- 隧道
// 传输层
LAYER_TYPE_UDP = 1 << 8,
LAYER_TYPE_TCP = 1 << 9,
LAYER_TYPE_L4 = (LAYER_TYPE_UDP | LAYER_TYPE_TCP),
// 传输层 -- 隧道
LAYER_TYPE_G_VXLAN = 1 << 10,
LAYER_TYPE_GTPV1_U = 1 << 11,
// ALL
LAYER_TYPE_ALL = (LAYER_TYPE_L2 | LAYER_TYPE_L2_TUN | LAYER_TYPE_L3 | LAYER_TYPE_L4 | LAYER_TYPE_G_VXLAN | LAYER_TYPE_GTPV1_U),
// UNKNOWN
LAYER_TYPE_UNKNOWN,
};
struct layer_record
{
enum layer_type type;
uint16_t hdr_offset;
uint16_t hdr_len;
uint16_t pld_len;
};
struct parser_result
{
struct layer_record layers[16];
uint16_t used;
uint16_t size;
};
struct packet_parser
{
struct parser_result result;
const void *packet_data;
uint16_t packet_len;
uint64_t packet_id;
};
void packet_parser_init(struct packet_parser *handler);
const void *packet_parser_parse(struct packet_parser *handler, const void *packet_data, uint16_t packet_len, uint64_t packet_id); // return most inner payload
const struct layer_record *packet_parser_get_most_inner(struct packet_parser *handler, enum layer_type type);
const struct layer_record *packet_parser_get_most_outer(struct packet_parser *handler, enum layer_type type);
// return 4: IPv4
// return 6: IPv6
uint8_t gtp_next_proto(const char *data);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -1,38 +0,0 @@
#ifndef _PUBLIC_H
#define _PUBLIC_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define IP_GET_RAW_VER(raw_pkt) ((((raw_pkt)[0] & 0xf0) >> 4))
#define LOG_DEBUG(format, ...) \
{ \
fprintf(stdout, format "\n", ##__VA_ARGS__); \
fflush(stdout); \
}
#define LOG_ERROR(format, ...) \
{ \
fprintf(stderr, format "\n", ##__VA_ARGS__); \
fflush(stderr); \
}
#ifdef __cpluscplus
}
#endif
#endif

61
common/include/utils.h Normal file
View File

@@ -0,0 +1,61 @@
#ifndef _UTILS_H
#define _UTILS_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <stdint.h>
#include <stdio.h>
struct metrics
{
uint64_t rx_pkts;
uint64_t rx_bytes;
uint64_t rx_err_pkts;
uint64_t rx_err_bytes;
uint64_t succ_tx_v4_pkts;
uint64_t succ_tx_v4_bytes;
uint64_t succ_tx_v6_pkts;
uint64_t succ_tx_v6_bytes;
uint64_t err_tx_v4_pkts;
uint64_t err_tx_v4_bytes;
uint64_t err_tx_v6_pkts;
uint64_t err_tx_v6_bytes;
};
inline void metrics_dump(struct metrics *metrics)
{
printf(" rx_pkts : %ld\n", metrics->rx_pkts);
printf(" rx_bytes : %ld\n", metrics->rx_bytes);
printf(" rx_err_pkts : %ld\n", metrics->rx_err_pkts);
printf(" rx_err_bytes : %ld\n", metrics->rx_err_bytes);
printf(" succ_tx_v4_pkts : %ld\n", metrics->succ_tx_v4_pkts);
printf(" succ_tx_v4_bytes : %ld\n", metrics->succ_tx_v4_bytes);
printf(" succ_tx_v6_pkts : %ld\n", metrics->succ_tx_v6_pkts);
printf(" succ_tx_v6_bytes : %ld\n", metrics->succ_tx_v6_bytes);
printf(" err_tx_v4_pkts : %ld\n", metrics->err_tx_v4_pkts);
printf(" err_tx_v4_bytes : %ld\n", metrics->err_tx_v4_bytes);
printf(" err_tx_v6_pkts : %ld\n", metrics->err_tx_v6_pkts);
printf(" err_tx_v6_bytes : %ld\n", metrics->err_tx_v6_bytes);
}
#define ATOMIC_READ(x) __atomic_fetch_add(x, 0, __ATOMIC_RELAXED)
#define ATOMIC_ADD(x, y) __atomic_fetch_add(x, y, __ATOMIC_RELAXED)
#ifdef __cpluscplus
}
#endif
#endif