This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
tango-kni/common/include/kni_utils.h

184 lines
5.0 KiB
C
Raw Normal View History

2019-05-09 15:14:01 +08:00
#pragma once
#include <stdio.h>
#include <stdlib.h>
2019-05-17 17:04:50 +08:00
#include <string.h>
2019-05-09 15:14:01 +08:00
#include <assert.h>
2019-06-17 20:52:22 +08:00
#include <errno.h>
2019-05-09 15:14:01 +08:00
#include <unistd.h>
2019-06-17 20:52:22 +08:00
#include <pthread.h>
2019-05-09 15:14:01 +08:00
#include <arpa/inet.h>
#include <time.h>
#include "MESA/MESA_handle_logger.h"
#include "MESA/MESA_htable.h"
#include "MESA/MESA_prof_load.h"
#include "field_stat2.h"
#include "Maat_rule.h"
#include "Maat_command.h"
2019-05-21 17:14:07 +08:00
#include "mrtunnat.h"
2019-09-07 20:21:50 +08:00
#include <sys/ioctl.h>
#include <netinet/ip6.h>
#include <net/if.h>
2019-05-09 15:14:01 +08:00
#define KNI_STRING_MAX 2048
#define KNI_PATH_MAX 256
#define KNI_SYMBOL_MAX 64
2019-05-17 17:04:50 +08:00
#define KNI_DOMAIN_MAX 256
#define KNI_ADDR_MAX 128
2019-06-05 11:32:11 +08:00
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif
2019-05-17 17:04:50 +08:00
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
2019-05-09 15:14:01 +08:00
#define likely(expr) __builtin_expect((expr), 1)
#define unlikely(expr) __builtin_expect((expr), 0)
#define ALLOC(type, number) ((type *)calloc(sizeof(type), number))
#define FREE(p) {free(*p);*p=NULL;}
#define KNI_LOG_ERROR(handler, fmt, ...) \
do { \
MESA_handle_runtime_log(handler, RLOG_LV_FATAL, "kni", fmt, ##__VA_ARGS__); } while(0)
2019-05-09 15:14:01 +08:00
#define KNI_LOG_INFO(handler, fmt, ...) \
do { \
MESA_handle_runtime_log(handler, RLOG_LV_INFO, "kni", fmt, ##__VA_ARGS__); } while(0)
2019-05-09 15:14:01 +08:00
#define KNI_LOG_DEBUG(handler, fmt, ...) \
do { \
MESA_handle_runtime_log(handler, RLOG_LV_DEBUG, "kni", fmt, ##__VA_ARGS__); } while(0)
2019-05-09 15:14:01 +08:00
2019-05-18 12:41:31 +08:00
2019-05-17 17:04:50 +08:00
//default tcp opt
#define KNI_DEFAULT_WINSCLE 0
#define KNI_DEFAULT_MSS 1460
#define KNI_DEFAULT_MTU 1500
#define KNI_MTU 3000
struct kni_tcpopt_info{
uint16_t mss;
uint8_t wscale_set;
2019-05-17 17:04:50 +08:00
uint8_t wscale;
uint8_t ts_set;
2019-05-17 17:04:50 +08:00
uint8_t sack;
uint32_t ts_value;
2019-05-17 17:04:50 +08:00
};
2019-05-18 12:41:31 +08:00
//field_stat
#define KNI_FS_FIELD_MAX 64
#define KNI_FS_COLUMN_MAX 256
#define KNI_FS_LINE_MAX 256
2019-05-18 12:41:31 +08:00
enum kni_field{
KNI_FIELD_INTCP_STM,
KNI_FIELD_BYP_STM,
KNI_FIELD_BYP_STM_POLICY,
KNI_FIELD_BYP_STM_PME_NEW_FAIL,
KNI_FIELD_BYP_STM_NO_TFE,
KNI_FIELD_BYP_STM_ERR,
KNI_FIELD_BYP_STM_DUP_TFC,
KNI_FIELD_STATE_UNKNOWN,
KNI_FIELD_DUP_TFC_STM,
//stream error
KNI_FIELD_STM_ERR,
KNI_FIELD_NO_SYN,
KNI_FIELD_SINGLE_DIR,
KNI_FIELD_PROTO_UNKNOWN,
KNI_FIELD_NO_SA,
KNI_FIELD_ACTION_INVALID,
KNI_FIELD_NO_DATA,
KNI_FIELD_IPHDR_PARSE_FAIL,
KNI_FIELD_EXCEED_MTU,
KNI_FIELD_STMERR_TUPLE2STM_ADD_FAIL,
KNI_FIELD_SENDTO_TFE_FAIL,
//others
KNI_FIELD_NULL_PKT,
KNI_FIELD_IPV4_STM,
KNI_FIELD_IPV6_STM,
2019-05-18 12:41:31 +08:00
KNI_FIELD_SSL_STM,
KNI_FIELD_HTTP_STM,
KNI_FIELD_SENDLOG_SUCC,
KNI_FIELD_SENDLOG_FAIL,
KNI_FIELD_PME_NEW_SUCC,
KNI_FIELD_PME_FREE,
KNI_FIELD_IPV4HDR_PARSE_FAIL,
KNI_FIELD_IPV6HDR_PARSE_FAIL,
KNI_FIELD_ID2PME_ADD_SUCC,
KNI_FIELD_ID2PME_ADD_FAIL,
KNI_FIELD_ID2PME_DEL_SUCC,
KNI_FIELD_ID2PME_DEL_FAIL,
KNI_FIELD_TUPLE2STM_ADD_SUCC,
KNI_FIELD_TUPLE2STM_ADD_FAIL,
KNI_FIELD_TUPLE2STM_DEL_SUCC,
KNI_FIELD_TUPLE2STM_DEL_FAIL,
2019-08-11 15:18:55 +08:00
KNI_FIELD_KNI_INTCP_BYTES,
KNI_FIELD_TFE_INTCP_BYTES,
KNI_FIELD_KNI_INTCP_STM,
KNI_FIELD_TFE_INTCP_STM,
KNI_FIELD_TUPLE2STM_SEARCH_SUCC,
KNI_FIELD_TUPLE2STM_SEARCH_FAIL,
KNI_FIELD_SAPP_INJECT_SUCC,
KNI_FIELD_SAPP_INJECT_FAIL,
KNI_FIELD_BLOOM_SEARCH_SUCC,
KNI_FIELD_BLOOM_SEARCH_FAIL,
KNI_FIELD_BLOOM_ADD_SUCC,
KNI_FIELD_BLOOM_ADD_FAIL,
//KNI_FIELD_TFE_STATUS_BASE must be last
KNI_FIELD_TFE_STATUS_BASE,
2019-05-18 12:41:31 +08:00
};
struct kni_field_stat_handle{
screen_stat_handle_t handle;
int fields[KNI_FS_FIELD_MAX];
int column_ids[KNI_FS_COLUMN_MAX];
int line_ids[KNI_FS_LINE_MAX];
int column_cnt;
int line_cnt;
2019-05-18 12:41:31 +08:00
};
2019-06-14 11:13:15 +08:00
struct pkt_info{
addr_type_t addr_type;
2019-06-14 11:13:15 +08:00
union{
struct iphdr *v4;
struct ip6_hdr *v6;
}iphdr;
uint16_t iphdr_len;
uint16_t ip_totlen;
struct tcphdr *tcphdr;
uint16_t tcphdr_len;
char *data;
uint16_t data_len;
};
enum kni_ipv4hdr_parse_error{
KNI_IPV4HDR_PARSE_ERROR_NULL_PACKET = -1,
};
enum kni_ipv6hdr_parse_error{
KNI_IPV6HDR_PARSE_ERROR_NULL_PACKET = -1,
KNI_IPV6HDR_PARSE_ERROR_NO_TCPHDR = -2,
KNI_IPV6HDR_PARSE_ERROR_INVALID_TYPE = -3,
};
2019-09-06 16:50:37 +08:00
enum kni_deploy_mode{
KNI_DEPLOY_MODE_TUN = 0,
KNI_DEPLOY_MODE_NORMAL = 1,
};
int kni_addr_trans_v4(struct stream_tuple4_v4 *tuple4, char *output, int len);
int kni_addr_trans_v6(struct stream_tuple4_v6 *tuple4, char *output, int len);
2019-05-17 17:04:50 +08:00
uint16_t kni_ip_checksum(const void *buf, size_t hdr_len);
uint16_t kni_tcp_checksum(const void *_buf, size_t len, in_addr_t src_addr, in_addr_t dest_addr);
2019-06-14 11:13:15 +08:00
uint16_t kni_tcp_checksum_v6(const void *_buf, size_t len, struct in6_addr src_addr, struct in6_addr dest_addr);
2019-05-17 17:04:50 +08:00
uint16_t kni_udp_checksum(const void *_buf, size_t len, in_addr_t src_addr, in_addr_t dest_addr);
2019-06-14 11:13:15 +08:00
struct kni_tcpopt_info* kni_get_tcpopt(struct tcphdr* tcphdr, int tcphdr_len);
int kni_ipv4_addr_get_by_eth(const char *ifname, uint32_t *ip);
2019-06-14 11:13:15 +08:00
int kni_ipv4_header_parse(const void *a_packet, struct pkt_info *pktinfo);
int kni_ipv6_header_parse(const void *a_packet, struct pkt_info *pktinfo);
char* kni_ipv4_errmsg_get(enum kni_ipv4hdr_parse_error _errno);
char* kni_ipv6_errmsg_get(enum kni_ipv6hdr_parse_error _errno);
2019-05-17 17:04:50 +08:00
MESA_htable_handle kni_create_htable(const char *profile, const char *section, void *free_data_cb, void *expire_notify_cb, void *logger);