2018-08-22 14:50:00 +08:00
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
2018-12-01 17:51:39 +08:00
# include <assert.h>
2018-08-22 14:50:00 +08:00
# include <sys/socket.h>
# include <arpa/inet.h>
# include <netinet/in.h>
# include <net/ethernet.h>
2019-01-23 16:57:11 +08:00
# include "http.h"
2018-10-31 20:17:52 +08:00
# include "kni_replace.h"
2018-08-22 14:50:00 +08:00
# include "kni_entry.h"
2018-11-30 18:25:46 +08:00
# include "kni_utils.h"
2018-12-29 18:08:23 +06:00
# include "kni_sendlog.h"
2018-08-22 14:50:00 +08:00
2019-01-23 19:49:14 +08:00
int g_kni_version_VERSION_20190123_2 ;
2018-08-22 14:50:00 +08:00
struct kni_var_comm g_kni_comminfo ;
struct kni_var_struct g_kni_structinfo ;
struct kni_var_maat g_kni_maatinfo ;
struct kni_fs2_info g_kni_fs2_info ;
struct kni_switch_info g_kni_switch_info ;
char g_kni_cardname [ KNI_CARD_NUM ] [ KNI_CONF_MAXLEN ] ;
int g_kni_threadseq [ KNI_MAX_THREADNUM ] ;
2019-01-25 15:30:53 +06:00
struct kni_pme_info * kni_pmeinfo_new ( void )
2018-08-22 14:50:00 +08:00
{
2018-11-30 18:25:46 +08:00
struct kni_pme_info * pmeinfo = ALLOC ( struct kni_pme_info , 1 ) ;
2018-11-30 17:25:48 +08:00
pmeinfo - > tun_index = - 1 ;
2018-08-22 14:50:00 +08:00
2019-01-25 15:30:53 +06:00
return pmeinfo ;
2018-08-22 14:50:00 +08:00
}
2019-01-25 15:30:53 +06:00
int kni_free_pmeinfo ( struct kni_pme_info * pmeinfo )
2018-08-22 14:50:00 +08:00
{
Maat_clean_status ( & ( pmeinfo - > mid ) ) ;
free ( pmeinfo ) ;
pmeinfo = NULL ;
return 0 ;
}
/***************************************************************************************
return : state_flag
kni_bmd : STAT_FLAG_SNIBMD
not kni_bmd : STAT_FLAG_SSL_NOBMD
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2018-10-31 20:17:52 +08:00
int kni_scan_domain ( char * domain , int domain_len , int thread_seq , struct kni_pme_info * pmeinfo )
2018-08-22 14:50:00 +08:00
{
int string_scan_num = 0 ;
int found_pos ;
2018-12-11 23:16:41 +08:00
string_scan_num = Maat_full_scan_string ( g_kni_maatinfo . maat_feather , g_kni_maatinfo . tableid_domain , CHARSET_GBK , domain , domain_len , & ( pmeinfo - > maat_result [ pmeinfo - > maat_result_num ] ) , & found_pos , KNI_MAX_SAMENUM - pmeinfo - > maat_result_num , & ( pmeinfo - > mid ) , thread_seq ) ;
kni_process_maatresult ( string_scan_num , & ( pmeinfo - > maat_result [ pmeinfo - > maat_result_num ] ) , pmeinfo ) ;
2018-08-22 14:50:00 +08:00
2018-11-13 18:23:53 +08:00
if ( string_scan_num < = 0 )
{
2018-12-11 23:16:41 +08:00
string_scan_num = Maat_full_scan_string ( g_kni_maatinfo . ipd_dyn_maat_feather , g_kni_maatinfo . tableid_dynamic_domain , CHARSET_GBK , domain , domain_len , & ( pmeinfo - > maat_result [ pmeinfo - > maat_result_num ] ) , & found_pos , KNI_MAX_SAMENUM - pmeinfo - > maat_result_num , & ( pmeinfo - > mid ) , thread_seq ) ;
kni_process_maatresult ( string_scan_num , & ( pmeinfo - > maat_result [ pmeinfo - > maat_result_num ] ) , pmeinfo ) ;
if ( string_scan_num > 0 )
{
pmeinfo - > maat_result_num + = string_scan_num ;
}
}
else
{
pmeinfo - > maat_result_num + = string_scan_num ;
2018-11-13 18:23:53 +08:00
}
2018-10-31 20:17:52 +08:00
return string_scan_num ;
2018-08-22 14:50:00 +08:00
}
2018-10-31 20:17:52 +08:00
/***************************************************************************************
return : state_flag
kni_bmd : STAT_FLAG_SNIBMD
not kni_bmd : STAT_FLAG_SSL_NOBMD
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2018-12-29 18:08:23 +06:00
/*
2018-10-31 20:17:52 +08:00
int kni_scan_pktbin ( char * data , int datalen , int thread_seq , struct kni_pme_info * pmeinfo )
{
int string_scan_num = 0 ;
int found_pos ;
struct Maat_rule_t maat_result [ KNI_MAX_SAMENUM ] ;
2018-08-22 14:50:00 +08:00
2018-12-29 18:08:23 +06:00
= Maat_full_scan_string ( g_kni_maatinfo . maat_feather , g_kni_maatinfo . tableid_pktbin , CHARSET_GBK , data , datalen , maat_result , & found_pos , KNI_MAX_SAMENUM , & ( pmeinfo - > mid ) , thread_seq ) ;
2018-10-31 20:17:52 +08:00
kni_process_maatresult ( string_scan_num , maat_result , pmeinfo ) ;
return string_scan_num ;
}
2018-12-29 18:08:23 +06:00
*/
2018-08-22 14:50:00 +08:00
2018-12-29 18:08:23 +06:00
int kni_scan_pktbin ( char * data , int datalen , int thread_seq , struct kni_pme_info * pmeinfo )
{
int string_scan_num = 0 ;
int found_pos ;
string_scan_num = Maat_full_scan_string ( g_kni_maatinfo . maat_feather , g_kni_maatinfo . tableid_pktbin ,
CHARSET_GBK , data , datalen , & ( pmeinfo - > maat_result [ pmeinfo - > maat_result_num ] ) ,
& found_pos , KNI_MAX_SAMENUM - pmeinfo - > maat_result_num , & ( pmeinfo - > mid ) , thread_seq ) ;
kni_process_maatresult ( string_scan_num , & ( pmeinfo - > maat_result [ pmeinfo - > maat_result_num ] ) , pmeinfo ) ;
if ( string_scan_num > = 0 )
{
pmeinfo - > maat_result_num + = string_scan_num ;
}
return string_scan_num ;
}
2018-08-22 14:50:00 +08:00
/***************************************************************************************
return : action
default : ipscan_num = 0 or = 1 , not > 1
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2018-10-31 20:17:52 +08:00
int kni_scan_ip ( struct ipaddr * addr , int thread_seq , int protocol , struct kni_pme_info * pmeinfo )
2018-08-22 14:50:00 +08:00
{
2018-10-31 20:17:52 +08:00
int ipscan_num = 0 ;
2018-08-22 14:50:00 +08:00
2018-12-11 23:16:41 +08:00
ipscan_num = Maat_scan_proto_addr ( g_kni_maatinfo . maat_feather , g_kni_maatinfo . tableid_ip , addr , protocol , & ( pmeinfo - > maat_result [ pmeinfo - > maat_result_num ] ) , KNI_MAX_SAMENUM - pmeinfo - > maat_result_num , & ( pmeinfo - > mid ) , thread_seq ) ;
kni_process_maatresult ( ipscan_num , & ( pmeinfo - > maat_result [ pmeinfo - > maat_result_num ] ) , pmeinfo ) ;
if ( ipscan_num > = 0 )
{
pmeinfo - > maat_result_num + = ipscan_num ;
}
2018-08-22 14:50:00 +08:00
2018-10-31 20:17:52 +08:00
//20181030add ,ipscan_action is monitor,use this keyringid;ipscan_action is replace,udp data not ipscan and pktscan
pmeinfo - > ipsscan_action = pmeinfo - > action ;
//end
2018-11-22 10:04:34 +08:00
2018-10-31 20:17:52 +08:00
return ipscan_num ;
2018-08-22 14:50:00 +08:00
}
2018-11-27 19:26:40 +08:00
void kni_free_htable ( void * htable_data )
{
struct kni_htable_datainfo * datainfo = ( struct kni_htable_datainfo * ) htable_data ;
if ( datainfo ! = NULL )
{
free ( datainfo ) ;
datainfo = NULL ;
}
return ;
}
2019-01-23 19:49:14 +08:00
int kni_htable_del ( const struct streaminfo * pstream , struct kni_pme_info * pmeinfo , const void * a_packet )
{
int ret = 0 ;
if ( ( pstream - > addr . addrtype = = ADDR_TYPE_IPV4 ) )
{
ret = MESA_htable_del ( g_kni_structinfo . htable_to_tun_v4 , ( unsigned char * ) & ( pmeinfo - > ipv4_addr ) , sizeof ( struct stream_tuple4_v4 ) , kni_free_htable ) ;
if ( ret < 0 )
{
kni_log_debug ( RLOG_LV_FATAL , ( char * ) " MESA_htable_del " , a_packet , " IPv4 MESA_htable_del() error,ret:%d " , ret ) ;
return - 1 ;
}
else
{
kni_filestate2_set ( pstream - > threadnum , FS_HTABLE_DEL , 0 , 1 ) ;
}
}
else
{
ret = MESA_htable_del ( g_kni_structinfo . htable_to_tun_v6 , ( unsigned char * ) & ( pmeinfo - > ipv6_addr ) , sizeof ( struct stream_tuple4_v6 ) , kni_free_htable ) ;
if ( ret < 0 )
{
kni_log_debug ( RLOG_LV_FATAL , ( char * ) " MESA_htable_del " , a_packet , " IPv6 MESA_htable_del() error,ret:%d " , ret ) ;
return - 1 ;
}
else
{
kni_filestate2_set ( pstream - > threadnum , FS_HTABLE_DEL , 0 , 1 ) ;
}
}
return 0 ;
}
2018-11-27 19:26:40 +08:00
2018-09-19 10:26:39 +08:00
int kni_htable_add ( const struct streaminfo * pstream , const void * a_packet , struct kni_pme_info * pmeinfo )
2018-08-22 14:50:00 +08:00
{
2018-11-26 09:10:54 +08:00
int ret = 0 ;
2018-08-22 14:50:00 +08:00
int iprevers = 0 ;
2018-09-19 10:26:39 +08:00
struct layer_addr_mac * mac_addr = ( struct layer_addr_mac * ) ( ( char * ) a_packet - KNI_ETHER_LEN ) ;
2018-11-30 18:25:46 +08:00
struct kni_htable_datainfo * datainfo = ALLOC ( struct kni_htable_datainfo , 1 ) ;
2018-08-22 14:50:00 +08:00
2018-09-19 10:26:39 +08:00
2018-11-26 09:10:54 +08:00
//send pkt info by self
2018-08-22 14:50:00 +08:00
if ( iprevers = = 0 )
{
datainfo - > route_dir = pstream - > routedir ;
2018-11-30 18:25:46 +08:00
memcpy ( datainfo - > smac , mac_addr - > src_mac , sizeof ( datainfo - > smac ) ) ;
memcpy ( datainfo - > dmac , mac_addr - > dst_mac , sizeof ( datainfo - > dmac ) ) ;
2018-08-22 14:50:00 +08:00
}
else
{
2018-10-31 20:17:52 +08:00
if ( g_kni_switch_info . sendpkt_mode = = 1 )
{
datainfo - > route_dir = 1 - pstream - > routedir ;
}
else
{
datainfo - > route_dir = MESA_dir_reverse ( pstream - > routedir ) ;
}
2018-11-30 18:25:46 +08:00
memcpy ( datainfo - > smac , mac_addr - > dst_mac , sizeof ( datainfo - > smac ) ) ;
memcpy ( datainfo - > dmac , mac_addr - > src_mac , sizeof ( datainfo - > dmac ) ) ;
2018-08-22 14:50:00 +08:00
}
//send wnd_pro_reply info
2018-11-30 18:25:46 +08:00
memcpy ( & ( datainfo - > lastpkt_info ) , & ( pmeinfo - > lastpkt_info ) , sizeof ( datainfo - > lastpkt_info ) ) ;
2018-08-22 14:50:00 +08:00
2018-09-19 10:26:39 +08:00
if ( pstream - > addr . addrtype = = ADDR_TYPE_IPV4 )
{
2019-01-23 19:49:14 +08:00
iprevers = kni_get_ipaddr_v4 ( ( void * ) a_packet , & ( pmeinfo - > ipv4_addr ) ) ;
ret = MESA_htable_add ( g_kni_structinfo . htable_to_tun_v4 , ( unsigned char * ) & ( pmeinfo - > ipv4_addr ) , sizeof ( struct stream_tuple4_v4 ) , ( void * ) datainfo ) ;
2018-11-27 19:26:40 +08:00
if ( ret = = MESA_HTABLE_RET_DUP_ITEM )
2018-11-26 09:10:54 +08:00
{
2019-01-25 15:30:53 +06:00
kni_log_debug ( RLOG_LV_FATAL , " kni_htable_add dup " , a_packet , " -5 " ) ;
2019-01-23 16:57:11 +08:00
free ( datainfo ) ;
datainfo = NULL ;
2018-11-26 09:10:54 +08:00
return - 1 ;
}
2018-11-27 19:26:40 +08:00
else if ( ret < 0 )
{
2019-01-25 15:30:53 +06:00
kni_log_debug ( RLOG_LV_FATAL , " kni_htable_add " , a_packet , " IPv4 MESA_htable_add() error,ret:%d " , ret ) ;
2019-01-23 16:57:11 +08:00
free ( datainfo ) ;
datainfo = NULL ;
2018-11-27 19:26:40 +08:00
return - 1 ;
}
else
{
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( pstream - > threadnum , FS_HTABLE_ADD , 0 , 1 ) ;
2018-11-27 19:26:40 +08:00
}
2018-09-19 10:26:39 +08:00
}
else
{
2019-01-23 19:49:14 +08:00
iprevers = kni_get_ipaddr_v6 ( ( void * ) a_packet , & ( pmeinfo - > ipv6_addr ) ) ;
ret = MESA_htable_add ( g_kni_structinfo . htable_to_tun_v6 , ( unsigned char * ) & ( pmeinfo - > ipv6_addr ) , sizeof ( struct stream_tuple4_v6 ) , ( void * ) datainfo ) ;
2018-11-26 09:10:54 +08:00
if ( ret < 0 )
{
2018-11-27 19:26:40 +08:00
kni_log_debug ( RLOG_LV_FATAL , ( char * ) " kni_htable_add " , a_packet , " IPv6 MESA_htable_add() error,ret:%d " , ret ) ;
2019-01-23 16:57:11 +08:00
free ( datainfo ) ;
datainfo = NULL ;
2018-11-26 09:10:54 +08:00
return - 1 ;
}
2018-12-28 13:33:55 +08:00
else
{
kni_filestate2_set ( pstream - > threadnum , FS_HTABLE_ADD , 0 , 1 ) ;
}
2018-11-26 09:10:54 +08:00
2018-09-19 10:26:39 +08:00
}
2018-08-22 14:50:00 +08:00
return 0 ;
}
2018-11-27 19:26:40 +08:00
/*
int kni_htable_add ( const struct streaminfo * pstream , const void * a_packet , struct kni_pme_info * pmeinfo )
{
int ret = 0 ;
int iprevers = 0 ;
struct stream_tuple4_v4 ipv4_addr ;
struct stream_tuple4_v6 ipv6_addr ;
struct layer_addr_mac * mac_addr = ( struct layer_addr_mac * ) ( ( char * ) a_packet - KNI_ETHER_LEN ) ;
struct kni_htable_datainfo * datainfo = ( struct kni_htable_datainfo * ) malloc ( sizeof ( struct kni_htable_datainfo ) ) ;
memset ( datainfo , 0 , sizeof ( struct kni_htable_datainfo ) ) ;
//send pkt info by self
if ( iprevers = = 0 )
{
datainfo - > route_dir = pstream - > routedir ;
memcpy ( datainfo - > smac , mac_addr - > src_mac , MAC_ADDR_LEN ) ;
memcpy ( datainfo - > dmac , mac_addr - > dst_mac , MAC_ADDR_LEN ) ;
}
else
{
if ( g_kni_switch_info . sendpkt_mode = = 1 )
{
datainfo - > route_dir = 1 - pstream - > routedir ;
}
else
{
datainfo - > route_dir = MESA_dir_reverse ( pstream - > routedir ) ;
}
memcpy ( datainfo - > smac , mac_addr - > dst_mac , MAC_ADDR_LEN ) ;
memcpy ( datainfo - > dmac , mac_addr - > src_mac , MAC_ADDR_LEN ) ;
}
//send wnd_pro_reply info
// memcpy(&(datainfo->tcpopt_info),&(pmeinfo->tcpopt_info),KNI_DIR_DOUBLE*sizeof(struct kni_tcpopt_info));
memcpy ( & ( datainfo - > lastpkt_info ) , & ( pmeinfo - > lastpkt_info ) , KNI_DIR_DOUBLE * sizeof ( struct kni_wndpro_reply_info ) ) ;
if ( pstream - > addr . addrtype = = ADDR_TYPE_IPV4 )
{
iprevers = kni_get_ipaddr_v4 ( ( void * ) a_packet , & ipv4_addr ) ;
ret = MESA_htable_add ( g_kni_structinfo . htable_to_tun_v4 , ( unsigned char * ) & ipv4_addr , sizeof ( struct stream_tuple4_v4 ) , ( void * ) datainfo ) ;
if ( ret < 0 )
{
kni_log_debug ( RLOG_LV_FATAL , ( char * ) " kni_htable_add " , a_packet , " IPv4 MESA_htable_add() error,ret:%d " , ret ) ;
return - 1 ;
}
}
else
{
iprevers = kni_get_ipaddr_v6 ( ( void * ) a_packet , & ipv6_addr ) ;
ret = MESA_htable_add ( g_kni_structinfo . htable_to_tun_v6 , ( unsigned char * ) & ipv6_addr , sizeof ( struct stream_tuple4_v6 ) , ( void * ) datainfo ) ;
if ( ret < 0 )
{
kni_log_debug ( RLOG_LV_FATAL , ( char * ) " kni_htable_add " , a_packet , " IPv6 MESA_htable_add() error,ret:%d " , ret ) ;
return - 1 ;
}
}
return 0 ;
}
2018-08-22 14:50:00 +08:00
2018-11-27 19:26:40 +08:00
*/
2018-08-22 14:50:00 +08:00
/***************************************************************************************
return : state_flag
ssl : STAT_FLAG_SSL_NOBMD
not ssl : STAT_FLAG_NOTSSL
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2018-09-18 17:13:28 +08:00
int kni_judge_ssl ( int thread_seq , char * tcp_data , int tcp_datalen , char * sni , int * sni_len , int * clienthello_flag , int * sni_flag )
2018-08-22 14:50:00 +08:00
{
int ssl_header_len = 0 ;
char * ssl_header = NULL ;
unsigned char content_type = 0 ;
unsigned short version_in_header = 0 ;
unsigned short len_in_header = 0 ;
int ssl_body_len = 0 ;
char * ssl_body = NULL ;
unsigned char handshark_type = 0 ;
unsigned int len_in_body = 0 ;
unsigned short version_in_body = 0 ;
unsigned char session_id_len = 0 ;
unsigned short ciphersuite_len = 0 ;
unsigned char compression_method_len = 0 ;
2018-11-30 18:25:46 +08:00
int ext_offset = 0 ;
2018-08-22 14:50:00 +08:00
char * ssl_extention = NULL ;
unsigned short extension_len_less = 0 ;
unsigned short type_in_extension = 0 ;
2018-11-30 18:25:46 +08:00
unsigned short ext_len = 0 ;
2018-08-22 14:50:00 +08:00
2019-01-11 00:00:29 +08:00
// unsigned short sni_list_len=0;
// unsigned char sni_type=0;
2018-08-22 14:50:00 +08:00
//ssl header
ssl_header = tcp_data ;
content_type = * ( unsigned char * ) & ssl_header [ ssl_header_len ] ;
if ( content_type ! = SSL_CONTENTTYPE_HANDSHAKE )
{
return KNI_FLAG_UNKNOW ;
}
ssl_header_len + = 1 ;
version_in_header = ntohs ( * ( unsigned short * ) & ( ssl_header [ ssl_header_len ] ) ) ;
if ( ( version_in_header ! = SSL_VERSION_TLS1_0 ) & & ( version_in_header ! = SSL_VERSION_TLS1_1 ) & & ( version_in_header ! = SSL_VERSION_TLS1_2 ) )
{
return KNI_FLAG_UNKNOW ;
}
ssl_header_len + = 2 ;
len_in_header = ntohs ( * ( unsigned short * ) & ( ssl_header [ ssl_header_len ] ) ) ;
if ( len_in_header ! = tcp_datalen - SSL_HEADER_LEN )
{
return KNI_FLAG_UNKNOW ;
}
ssl_header_len + = 2 ;
//ssl body
ssl_body = ssl_header + ssl_header_len ;
handshark_type = * ( unsigned char * ) & ( ssl_body [ ssl_body_len ] ) ;
if ( handshark_type ! = SSL_HANDSHAR_TYPE_CLIENTHELLO )
{
return KNI_FLAG_UNKNOW ;
}
ssl_body_len + = 1 ;
2018-09-18 17:13:28 +08:00
* clienthello_flag = 1 ;
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( thread_seq , FS_CLIENT_HELLO , 0 , 1 ) ;
2018-09-18 17:13:28 +08:00
2018-08-22 14:50:00 +08:00
// memcpy(&len_in_body,&ssl_body[ssl_body_len],3);
len_in_body = * ( unsigned char * ) & ssl_body [ ssl_body_len + 2 ] + 256 * ( * ( unsigned char * ) & ssl_body [ ssl_body_len + 1 ] ) + 65536 * ( * ( unsigned char * ) & ssl_body [ ssl_body_len ] ) ;
2019-01-11 10:27:38 +06:00
if ( len_in_body + SSL_BODY_LEN ! = len_in_header )
2018-08-22 14:50:00 +08:00
{
return KNI_FLAG_UNKNOW ;
}
ssl_body_len + = 3 ;
version_in_body = ntohs ( * ( unsigned short * ) & ( ssl_body [ ssl_body_len ] ) ) ;
if ( ( version_in_body ! = SSL_VERSION_TLS1_0 ) & & ( version_in_body ! = SSL_VERSION_TLS1_1 ) & & ( version_in_body ! = SSL_VERSION_TLS1_2 ) )
{
return KNI_FLAG_UNKNOW ;
}
ssl_body_len + = 2 ;
ssl_body_len + = 32 ; //4byte time,28bytes random
session_id_len = * ( unsigned char * ) & ( ssl_body [ ssl_body_len ] ) ;
ssl_body_len + = 1 ;
ssl_body_len + = session_id_len ;
ciphersuite_len = ntohs ( * ( unsigned short * ) & ( ssl_body [ ssl_body_len ] ) ) ;
ssl_body_len + = 2 ;
ssl_body_len + = ciphersuite_len ;
compression_method_len = * ( unsigned char * ) & ( ssl_body [ ssl_body_len ] ) ;
ssl_body_len + = 1 ;
ssl_body_len + = compression_method_len ;
//ssl extention
ssl_extention = ssl_body + ssl_body_len ;
2018-12-07 11:25:04 +06:00
if ( ssl_body - tcp_data + ssl_body_len > = tcp_datalen ) return KNI_FLAG_UNKNOW ;
2018-08-22 14:50:00 +08:00
2018-11-30 18:25:46 +08:00
extension_len_less = ntohs ( * ( unsigned short * ) & ssl_extention [ ext_offset ] ) ;
2018-08-22 14:50:00 +08:00
if ( extension_len_less ! = len_in_body - 2 - 32 - 1 - session_id_len - 2 - ciphersuite_len - 1 - compression_method_len - 2 )
{
return KNI_FLAG_UNKNOW ;
}
2018-11-30 18:25:46 +08:00
ext_offset + = 2 ;
2018-08-22 14:50:00 +08:00
2018-11-30 18:25:46 +08:00
while ( ext_offset < extension_len_less )
2018-08-22 14:50:00 +08:00
{
2018-11-30 18:25:46 +08:00
type_in_extension = ntohs ( * ( unsigned short * ) & ssl_extention [ ext_offset ] ) ;
ext_offset + = 2 ;
2018-08-22 14:50:00 +08:00
2018-11-30 18:25:46 +08:00
ext_len = ntohs ( * ( unsigned short * ) & ssl_extention [ ext_offset ] ) ;
ext_offset + = 2 ;
2019-01-11 00:00:29 +08:00
//sni
2018-08-22 14:50:00 +08:00
if ( type_in_extension = = SSL_EXTENSION_TYPE_SNI )
{
2018-09-18 17:13:28 +08:00
2018-11-30 18:25:46 +08:00
if ( ext_len > KNI_SNI_MAXLEN )
2018-08-22 14:50:00 +08:00
{
//error
return KNI_FLAG_UNKNOW ;
}
2019-01-11 00:00:29 +08:00
// memcpy(sni,&ssl_extention[ext_offset],ext_len);
// *sni_len=ext_len;
if ( ext_len > 5 )
2018-08-22 14:50:00 +08:00
{
2019-01-11 00:00:29 +08:00
// sni_list_len=ntohs(*(unsigned short*)&ssl_extention[ext_offset]);
ext_offset + = 2 ;
// sni_type = *(unsigned char*)&ssl_extention[ext_offset];
ext_offset + = 1 ;
* sni_len = ntohs ( * ( unsigned short * ) & ssl_extention [ ext_offset ] ) ;
ext_offset + = 2 ;
if ( ( * sni_len > 0 ) & & ( * sni_len = = ext_len - 5 ) )
{
if ( * sni_len > KNI_DEFAULT_MTU )
{
* sni_len = KNI_DEFAULT_MTU ;
}
* sni_flag = 1 ;
kni_filestate2_set ( thread_seq , FS_SNI , 0 , 1 ) ;
memcpy ( sni , & ssl_extention [ ext_offset ] , * sni_len ) ;
return KNI_FLAG_SSL ;
}
else
{
return KNI_FLAG_UNKNOW ;
}
}
else
{
* sni_flag = 0 ;
* sni_len = 0 ;
sni = NULL ;
return KNI_FLAG_UNKNOW ;
2018-08-22 14:50:00 +08:00
}
2019-01-11 00:00:29 +08:00
2018-08-22 14:50:00 +08:00
}
else
{
2018-11-30 18:25:46 +08:00
ext_offset + = ext_len ;
2018-08-22 14:50:00 +08:00
continue ;
}
}
return KNI_FLAG_UNKNOW ;
}
int kni_judge_http ( const struct streaminfo * stream , char * domain , int * domain_len )
{
int val = 1 ;
struct kni_http_project * host = ( struct kni_http_project * ) project_req_get_struct ( stream , g_kni_comminfo . project_id ) ;
if ( host = = NULL )
{
* domain_len = 0 ;
return - 1 ;
}
* domain_len = host - > host_len > KNI_DEFAULT_MTU ? KNI_DEFAULT_MTU : host - > host_len ;
memcpy ( domain , host - > host , * domain_len ) ;
return val ;
}
2018-09-19 10:26:39 +08:00
int kni_protocol_identify ( const struct streaminfo * pstream , const void * a_packet , char * tcp_data , int tcp_datalen , char * domain , int * domain_len )
2018-08-22 14:50:00 +08:00
{
2018-09-18 17:13:28 +08:00
int clienthello_flag = 0 ;
int sni_flag = 0 ;
2018-08-22 14:50:00 +08:00
if ( kni_judge_http ( pstream , domain , domain_len ) = = 1 )
{
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( pstream - > threadnum , FS_HTTP , 0 , 1 ) ;
2018-08-22 14:50:00 +08:00
return KNI_FLAG_HTTP ;
}
2018-09-18 17:13:28 +08:00
else if ( kni_judge_ssl ( pstream - > threadnum , tcp_data , tcp_datalen , domain , domain_len , & clienthello_flag , & sni_flag ) = = KNI_FLAG_SSL )
2018-08-22 14:50:00 +08:00
{
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( pstream - > threadnum , FS_SSL , 0 , 1 ) ;
2018-08-22 14:50:00 +08:00
return KNI_FLAG_SSL ;
}
2018-09-18 17:13:28 +08:00
else if ( ( clienthello_flag = = 1 ) & & ( sni_flag = = 0 ) )
{
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( pstream - > threadnum , FS_SSL , 0 , 1 ) ;
2018-11-28 17:29:43 +08:00
kni_log_debug ( RLOG_LV_DEBUG , ( char * ) " SSL_IDENTIFY " , ( void * ) a_packet , ( char * ) " this ssl has client_hello,but no sni! " ) ;
2018-10-31 20:17:52 +08:00
return KNI_FLAG_SSL ;
2018-09-18 17:13:28 +08:00
}
2018-08-22 14:50:00 +08:00
return KNI_FLAG_NOTPROC ;
}
2018-10-31 20:17:52 +08:00
2018-12-11 23:16:41 +08:00
char kni_process_udppkt ( unsigned char routdir , struct kni_pme_info * pmeinfo , int thread_seq , const void * a_packet , const struct streaminfo * pstream )
2018-10-31 20:17:52 +08:00
{
char ret = APP_STATE_FAWPKT | APP_STATE_DROPME ;
switch ( pmeinfo - > action )
{
case KNI_ACTION_RATELIMIT :
2018-12-11 23:16:41 +08:00
ret = kni_process_ratelimit ( thread_seq , pstream , a_packet , pmeinfo ) ;
2018-10-31 20:17:52 +08:00
break ;
case KNI_ACTION_REPLACE :
2018-12-11 23:16:41 +08:00
ret = kni_process_replace ( routdir , thread_seq , pstream , a_packet , pmeinfo ) ;
2018-10-31 20:17:52 +08:00
break ;
case KNI_ACTION_HALFHIT :
ret = APP_STATE_GIVEME | APP_STATE_FAWPKT ;
default :
break ;
}
return ret ;
}
2018-09-19 10:26:39 +08:00
char kni_first_tcpdata ( const struct streaminfo * pstream , const void * a_packet , struct kni_pme_info * pmeinfo , char * data , int datalen )
2018-11-27 19:26:40 +08:00
{
2018-11-23 00:14:36 +08:00
struct timespec start , end ;
long elapse = 0 ;
2018-08-22 14:50:00 +08:00
char ret = APP_STATE_FAWPKT | APP_STATE_DROPME ;
int domain_len = 0 ;
char domain [ KNI_DEFAULT_MTU ] = { 0 } ;
2018-12-19 10:48:25 +06:00
if ( pstream - > dir ! = DIR_DOUBLE )
{
2018-12-22 15:07:21 +06:00
kni_filestate2_set ( pstream - > threadnum , FS_NOT_DOUBLE , 0 , 1 ) ;
2018-12-19 10:48:25 +06:00
kni_log_info ( ( char * ) KNI_MODULE_INFO , & ( pstream - > addr ) , pmeinfo - > protocol , domain , ( char * ) " NOT-DOUBLE " , ( char * ) " BYPASS " , pmeinfo ) ;
return ret ;
}
2018-08-22 14:50:00 +08:00
2018-10-31 20:17:52 +08:00
pmeinfo - > protocol = kni_protocol_identify ( pstream , a_packet , data , datalen , domain , & domain_len ) ;
2018-12-04 22:29:50 +08:00
assert ( domain_len < ( int ) sizeof ( domain ) ) ;
2018-10-31 20:17:52 +08:00
if ( ( pmeinfo - > protocol = = KNI_FLAG_HTTP ) | | ( pmeinfo - > protocol = = KNI_FLAG_SSL ) )
2018-08-22 14:50:00 +08:00
{
2018-10-31 20:17:52 +08:00
if ( domain_len ! = 0 )
2018-08-22 14:50:00 +08:00
{
2018-10-31 20:17:52 +08:00
kni_scan_domain ( domain , domain_len , pstream - > threadnum , pmeinfo ) ;
2018-08-22 14:50:00 +08:00
}
2018-10-31 20:17:52 +08:00
switch ( pmeinfo - > action )
2018-08-22 14:50:00 +08:00
{
2018-10-31 20:17:52 +08:00
case KNI_ACTION_WHITELIST :
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( pstream - > threadnum , FS_WHITELIST , 0 , 1 ) ;
2018-11-08 18:58:51 +08:00
kni_log_info ( ( char * ) KNI_MODULE_INFO , & ( pstream - > addr ) , pmeinfo - > protocol , domain , ( char * ) " WHITE_LIST_DOMAIN " , ( char * ) " BYPASS " , pmeinfo ) ;
2018-10-31 20:17:52 +08:00
pmeinfo - > protocol = KNI_FLAG_NOTPROC ;
return ret ;
case KNI_ACTION_RATELIMIT :
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( pstream - > threadnum , FS_RATELIMIT , 0 , 1 ) ;
2018-11-08 18:58:51 +08:00
kni_log_info ( ( char * ) KNI_MODULE_INFO , & ( pstream - > addr ) , pstream - > type , NULL , ( char * ) " RATELIMITE " , ( char * ) " RATELIMITE " , pmeinfo ) ;
2018-12-11 23:16:41 +08:00
ret = kni_process_ratelimit ( pstream - > threadnum , pstream , a_packet , pmeinfo ) ;
2018-10-31 20:17:52 +08:00
return ret ;
case KNI_ACTION_NONE :
case KNI_ACTION_HALFHIT :
if ( g_kni_switch_info . maat_default_mode = = KNI_DEFAULT_MODE_BYPASS )
{
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( pstream - > threadnum , FS_NOT_HIT , 0 , 1 ) ;
2018-11-08 18:58:51 +08:00
kni_log_info ( ( char * ) KNI_MODULE_INFO , & ( pstream - > addr ) , pmeinfo - > protocol , domain , ( char * ) " NOT_HIT " , ( char * ) " BYPASS " , pmeinfo ) ;
2018-10-31 20:17:52 +08:00
pmeinfo - > protocol = KNI_FLAG_NOTPROC ;
pmeinfo - > action = KNI_ACTION_NONE ;
return ret ;
}
else
{
pmeinfo - > action = KNI_ACTION_MONITOR ;
}
2018-11-28 17:29:43 +08:00
break ;
2018-11-27 19:26:40 +08:00
default :
2018-11-28 17:29:43 +08:00
pmeinfo - > action = KNI_ACTION_MONITOR ;
break ;
2018-11-27 19:26:40 +08:00
2018-08-22 14:50:00 +08:00
}
2018-10-31 20:17:52 +08:00
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( pstream - > threadnum , FS_INTERCEPT , 0 , 1 ) ;
2018-11-26 09:10:54 +08:00
if ( kni_htable_add ( pstream , a_packet , pmeinfo ) < 0 )
{
2018-11-27 19:26:40 +08:00
pmeinfo - > action = KNI_ACTION_NOTPROC ;
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( pstream - > threadnum , FS_DROP_ADDHTABLE_ERROR , 0 , 1 ) ;
2018-11-26 09:10:54 +08:00
return ret ;
}
2018-11-23 00:14:36 +08:00
clock_gettime ( CLOCK_MONOTONIC , & start ) ;
2018-08-22 14:50:00 +08:00
2018-10-31 20:17:52 +08:00
if ( tcp_repair_process ( pstream , a_packet , pmeinfo , pmeinfo - > protocol ) < 0 )
2018-08-22 14:50:00 +08:00
{
2019-01-23 19:49:14 +08:00
kni_htable_del ( pstream , pmeinfo , a_packet ) ;
2018-11-27 19:26:40 +08:00
pmeinfo - > action = KNI_ACTION_NOTPROC ;
2018-11-23 00:14:36 +08:00
clock_gettime ( CLOCK_MONOTONIC , & end ) ;
elapse = ( end . tv_sec - start . tv_sec ) * 1000000 + ( end . tv_nsec - start . tv_nsec ) / 1000 ;
FS_operate ( g_kni_fs2_info . handler , g_kni_fs2_info . metric_tcprepair , 0 , FS_OP_SET , elapse ) ;
2018-08-22 14:50:00 +08:00
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_INFO , " tcp_repair_process " , " tcp_repair_process() error! " ) ;
return ret ;
}
2019-01-25 15:30:53 +06:00
else
{
pmeinfo - > is_tcp_repaired = 1 ;
}
2018-11-22 10:04:34 +08:00
2018-11-23 00:14:36 +08:00
clock_gettime ( CLOCK_MONOTONIC , & end ) ;
elapse = ( end . tv_sec - start . tv_sec ) * 1000000 + ( end . tv_nsec - start . tv_nsec ) / 1000 ;
FS_operate ( g_kni_fs2_info . handler , g_kni_fs2_info . metric_tcprepair , 0 , FS_OP_SET , elapse ) ;
2018-11-08 18:58:51 +08:00
kni_log_info ( ( char * ) KNI_MODULE_INFO , & ( pstream - > addr ) , pmeinfo - > protocol , domain , ( char * ) " INTERCEPT " , ( char * ) " INTERCEPT " , pmeinfo ) ;
2018-08-22 14:50:00 +08:00
ret = APP_STATE_DROPPKT | APP_STATE_GIVEME ;
}
else
{
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( pstream - > threadnum , FS_NOT_HTTP_SSL , 0 , 1 ) ;
2018-11-08 18:58:51 +08:00
kni_log_info ( ( char * ) KNI_MODULE_INFO , & ( pstream - > addr ) , pmeinfo - > protocol , NULL , ( char * ) " NOT_HTTP_SSL " , ( char * ) " BYPASS " , pmeinfo ) ;
2018-08-22 14:50:00 +08:00
}
return ret ;
}
2018-09-19 10:26:39 +08:00
char kni_pending_opstate ( const struct streaminfo * pstream , struct kni_pme_info * pmeinfo , int thread_seq , const void * a_packet , int protocol )
2018-08-22 14:50:00 +08:00
{
char ret = APP_STATE_FAWPKT | APP_STATE_DROPME ;
char * data = NULL ;
int datalen = 0 ;
2018-09-19 10:26:39 +08:00
int iplen = 0 ;
int tcphdr_len = 0 ;
2018-08-22 14:50:00 +08:00
2018-09-19 10:26:39 +08:00
struct ip * ipv4_hdr = NULL ;
struct kni_ipv6_hdr * ipv6_hdr = NULL ;
struct kni_tcp_hdr * tcphdr = NULL ;
2018-11-26 09:10:54 +08:00
data = kni_get_payload ( pstream , & datalen ) ;
2018-09-19 10:26:39 +08:00
if ( pstream - > addr . addrtype = = ADDR_TYPE_IPV4 )
{
ipv4_hdr = ( struct ip * ) a_packet ;
iplen = ntohs ( ipv4_hdr - > ip_len ) ;
tcphdr_len = iplen - 4 * ( ipv4_hdr - > ip_hl ) - datalen ;
tcphdr = ( struct kni_tcp_hdr * ) ( ( char * ) ipv4_hdr + 4 * ( ipv4_hdr - > ip_hl ) ) ;
}
else if ( pstream - > addr . addrtype = = ADDR_TYPE_IPV6 )
{
ipv6_hdr = ( struct kni_ipv6_hdr * ) a_packet ;
iplen = ntohs ( ipv6_hdr - > ip6_payload_len ) + sizeof ( struct kni_ipv6_hdr ) ;
tcphdr_len = ntohs ( ipv6_hdr - > ip6_payload_len ) - datalen ;
tcphdr = ( struct kni_tcp_hdr * ) ( ( unsigned char * ) a_packet + sizeof ( struct kni_ipv6_hdr ) ) ;
}
else
{
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( thread_seq , FS_DROP_NOTIPV46_SAPP , 0 , 1 ) ;
2018-09-19 10:26:39 +08:00
return ret ;
}
2018-08-22 14:50:00 +08:00
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( thread_seq , FS_RX_BYTES , 0 , iplen ) ;
2018-10-31 20:17:52 +08:00
kni_scan_ip ( ( struct ipaddr * ) & ( pstream - > addr ) , thread_seq , protocol , pmeinfo ) ;
if ( pmeinfo - > action = = KNI_ACTION_WHITELIST )
2018-08-22 14:50:00 +08:00
{
2018-11-08 18:58:51 +08:00
kni_log_info ( ( char * ) KNI_MODULE_INFO , & ( pstream - > addr ) , 0 , NULL , ( char * ) " WHITE_LIST_IP " , ( char * ) " BYPASS " , pmeinfo ) ;
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( thread_seq , FS_WHITELIST , 0 , 1 ) ;
2018-08-22 14:50:00 +08:00
return ret ;
}
2018-12-19 10:48:25 +06:00
2018-12-17 08:13:46 +08:00
//add kni_action_redirect 20181216 start
2018-12-19 10:48:25 +06:00
else if ( redirect_search_htable ( pstream - > addr . addrtype , pmeinfo , thread_seq , a_packet , protocol ) = = 1 )
{
ret = process_redirect_data ( pstream , pmeinfo , thread_seq , a_packet , protocol , pstream - > routedir ) ;
return ret ;
}
2018-12-17 08:13:46 +08:00
else if ( pmeinfo - > action = = KNI_ACTION_REDIRECT )
{
ret = process_redirect_pending ( pstream , pmeinfo , thread_seq , a_packet , protocol , pstream - > routedir ) ;
return ret ;
}
2018-08-22 14:50:00 +08:00
2018-12-19 10:48:25 +06:00
//end
2018-10-31 20:17:52 +08:00
pmeinfo - > protocol = KNI_FLAG_UNKNOW ;
2018-08-22 14:50:00 +08:00
2018-10-31 20:17:52 +08:00
if ( protocol = = PROTO_TYPE_TCP )
{
2019-02-26 20:35:52 +06:00
kni_get_tcpopt ( tcphdr , tcphdr_len ,
& ( pmeinfo - > tcpopt_info [ pstream - > curdir - 1 ] . mss ) ,
& ( pmeinfo - > tcpopt_info [ pstream - > curdir - 1 ] . wscale_perm ) , & ( pmeinfo - > tcpopt_info [ pstream - > curdir - 1 ] . wscale ) ,
& ( pmeinfo - > tcpopt_info [ pstream - > curdir - 1 ] . sack_perm ) , & ( pmeinfo - > tcpopt_info [ pstream - > curdir - 1 ] . timestamps ) ) ;
2018-10-31 20:17:52 +08:00
kni_get_tcpinfo ( & ( pmeinfo - > lastpkt_info [ pstream - > curdir - 1 ] ) , tcphdr , datalen ) ;
2018-11-28 17:29:43 +08:00
if ( datalen > 0 ) //TODO:get link create mode from sapp
2018-08-22 14:50:00 +08:00
{
2018-10-31 20:17:52 +08:00
ret = kni_first_tcpdata ( pstream , a_packet , pmeinfo , data , datalen ) ;
2018-11-30 17:25:48 +08:00
if ( ( pmeinfo - > action = = KNI_ACTION_MONITOR ) & & ( ( pmeinfo - > protocol = = KNI_FLAG_HTTP ) | | ( pmeinfo - > protocol = = KNI_FLAG_SSL ) ) )
2018-10-31 20:17:52 +08:00
{
2018-11-27 19:26:40 +08:00
if ( g_kni_switch_info . write_listq_switch = = 1 )
{
2018-11-30 17:25:48 +08:00
if ( pmeinfo - > tun_index < 0 )
{
pmeinfo - > tun_index = random ( ) % g_kni_comminfo . tun_threadnum ;
}
2019-01-23 19:49:14 +08:00
ret = kni_add_lqueue ( ADDR_TYPE_IPV4 , thread_seq , ( char * ) ipv4_hdr , iplen , pstream , pmeinfo - > tun_index , pmeinfo ) ;
2018-11-27 19:26:40 +08:00
}
else
{
2019-01-23 19:49:14 +08:00
ret = tun_write_data ( g_kni_comminfo . fd_tun [ thread_seq ] , ( char * ) ipv4_hdr , iplen , ( struct streaminfo * ) pstream , thread_seq , pmeinfo ) ;
2018-11-27 19:26:40 +08:00
}
2018-10-31 20:17:52 +08:00
}
2018-08-22 14:50:00 +08:00
}
2018-10-31 20:17:52 +08:00
else
{
ret = APP_STATE_FAWPKT | APP_STATE_GIVEME ;
}
}
2018-08-22 14:50:00 +08:00
else
{
2018-10-31 20:17:52 +08:00
kni_scan_pktbin ( ( char * ) ( pstream - > pudpdetail - > pdata ) , pstream - > pudpdetail - > datalen , thread_seq , pmeinfo ) ;
2018-12-11 23:16:41 +08:00
ret = kni_process_udppkt ( pstream - > routedir , pmeinfo , thread_seq , a_packet , pstream ) ;
2018-08-22 14:50:00 +08:00
}
2018-10-31 20:17:52 +08:00
2018-08-22 14:50:00 +08:00
return ret ;
}
2018-10-31 20:17:52 +08:00
char kni_data_opstate ( const struct streaminfo * pstream , struct kni_pme_info * pmeinfo , int thread_seq , const void * a_packet , int protocol )
2018-08-22 14:50:00 +08:00
{
char ret = APP_STATE_DROPPKT | APP_STATE_GIVEME ;
char * data = NULL ;
int datalen = 0 ;
2018-09-19 10:26:39 +08:00
int iplen = 0 ;
int tcphdr_len = 0 ;
2018-08-22 14:50:00 +08:00
2018-09-19 10:26:39 +08:00
struct ip * ipv4_hdr = NULL ;
struct kni_ipv6_hdr * ipv6_hdr = NULL ;
struct kni_tcp_hdr * tcphdr = NULL ;
2018-08-22 14:50:00 +08:00
2018-12-17 08:13:46 +08:00
//add kni_action_redirect 20181216 start
if ( pmeinfo - > action = = KNI_ACTION_REDIRECT )
{
ret = process_redirect_data ( pstream , pmeinfo , thread_seq , a_packet , protocol , pstream - > routedir ) ;
return ret ;
}
//end
2018-11-26 09:10:54 +08:00
data = kni_get_payload ( pstream , & datalen ) ;
2018-09-19 10:26:39 +08:00
if ( pstream - > addr . addrtype = = ADDR_TYPE_IPV4 )
{
ipv4_hdr = ( struct ip * ) a_packet ;
iplen = ntohs ( ipv4_hdr - > ip_len ) ;
tcphdr_len = iplen - 4 * ( ipv4_hdr - > ip_hl ) - datalen ;
tcphdr = ( struct kni_tcp_hdr * ) ( ( char * ) ipv4_hdr + 4 * ( ipv4_hdr - > ip_hl ) ) ;
}
else if ( pstream - > addr . addrtype = = ADDR_TYPE_IPV6 )
{
ipv6_hdr = ( struct kni_ipv6_hdr * ) a_packet ;
iplen = ntohs ( ipv6_hdr - > ip6_payload_len ) + sizeof ( struct kni_ipv6_hdr ) ;
tcphdr_len = ntohs ( ipv6_hdr - > ip6_payload_len ) - datalen ;
tcphdr = ( struct kni_tcp_hdr * ) ( ( unsigned char * ) a_packet + sizeof ( struct kni_ipv6_hdr ) ) ;
}
else
{
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( thread_seq , FS_DROP_NOTIPV46_SAPP , 0 , 1 ) ;
2018-09-19 10:26:39 +08:00
return ret ;
}
2018-10-31 20:17:52 +08:00
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( thread_seq , FS_RX_BYTES , 0 , iplen ) ;
2018-10-31 20:17:52 +08:00
if ( protocol = = PROTO_TYPE_TCP )
2018-08-22 14:50:00 +08:00
{
2018-10-31 20:17:52 +08:00
if ( pmeinfo - > protocol = = KNI_FLAG_UNKNOW )
2018-08-22 14:50:00 +08:00
{
2018-10-31 20:17:52 +08:00
if ( ( tcphdr - > th_flags & TH_SYN ) & & ( tcphdr - > th_flags & TH_ACK ) )
{
2019-02-26 20:35:52 +06:00
kni_get_tcpopt ( tcphdr , tcphdr_len ,
& ( pmeinfo - > tcpopt_info [ pstream - > curdir - 1 ] . mss ) ,
& ( pmeinfo - > tcpopt_info [ pstream - > curdir - 1 ] . wscale_perm ) , & ( pmeinfo - > tcpopt_info [ pstream - > curdir - 1 ] . wscale ) ,
& ( pmeinfo - > tcpopt_info [ pstream - > curdir - 1 ] . sack_perm ) ,
& ( pmeinfo - > tcpopt_info [ pstream - > curdir - 1 ] . timestamps ) ) ;
2018-10-31 20:17:52 +08:00
}
kni_get_tcpinfo ( & ( pmeinfo - > lastpkt_info [ pstream - > curdir - 1 ] ) , tcphdr , datalen ) ;
if ( datalen > 0 )
{
ret = kni_first_tcpdata ( pstream , a_packet , pmeinfo , data , datalen ) ;
}
else
{
ret = APP_STATE_FAWPKT | APP_STATE_GIVEME ;
}
2018-08-22 14:50:00 +08:00
}
2018-10-31 20:17:52 +08:00
if ( ( pmeinfo - > action = = KNI_ACTION_MONITOR ) & & ( ( pmeinfo - > protocol = = KNI_FLAG_HTTP ) | | ( pmeinfo - > protocol = = KNI_FLAG_SSL ) ) )
2018-08-22 14:50:00 +08:00
{
2018-11-27 19:26:40 +08:00
if ( g_kni_switch_info . write_listq_switch = = 1 )
{
2018-11-30 17:25:48 +08:00
if ( pmeinfo - > tun_index < 0 )
{
pmeinfo - > tun_index = random ( ) % g_kni_comminfo . tun_threadnum ;
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , " get tun_index " , " index:%d " , pmeinfo - > tun_index ) ;
}
2019-01-23 19:49:14 +08:00
ret = kni_add_lqueue ( ADDR_TYPE_IPV4 , thread_seq , ( char * ) a_packet , iplen , pstream , pmeinfo - > tun_index , pmeinfo ) ;
2018-11-27 19:26:40 +08:00
}
else
{
2019-01-23 19:49:14 +08:00
ret = tun_write_data ( g_kni_comminfo . fd_tun [ thread_seq ] , ( char * ) a_packet , iplen , ( struct streaminfo * ) pstream , thread_seq , pmeinfo ) ;
2018-11-27 19:26:40 +08:00
}
2018-08-22 14:50:00 +08:00
}
2018-10-31 20:17:52 +08:00
else if ( pmeinfo - > action = = KNI_ACTION_RATELIMIT )
2018-08-22 14:50:00 +08:00
{
2018-12-11 23:16:41 +08:00
ret = kni_process_ratelimit ( pstream - > threadnum , pstream , ( void * ) a_packet , pmeinfo ) ;
2018-10-31 20:17:52 +08:00
return ret ;
2018-08-22 14:50:00 +08:00
}
}
2018-10-31 20:17:52 +08:00
else
2018-08-22 14:50:00 +08:00
{
2018-10-31 20:17:52 +08:00
if ( pmeinfo - > ipsscan_action = = KNI_ACTION_NONE )
{
kni_scan_ip ( ( struct ipaddr * ) & ( pstream - > addr ) , thread_seq , protocol , pmeinfo ) ;
}
2018-12-30 15:43:31 +08:00
if ( ( pmeinfo - > ipsscan_action ! = KNI_ACTION_REPLACE ) & & ( pmeinfo - > ipsscan_action ! = KNI_ACTION_RATELIMIT ) )
2018-10-31 20:17:52 +08:00
{
kni_scan_pktbin ( ( char * ) ( pstream - > pudpdetail - > pdata ) , pstream - > pudpdetail - > datalen , thread_seq , pmeinfo ) ;
}
2018-12-11 23:16:41 +08:00
ret = kni_process_udppkt ( pstream - > routedir , pmeinfo , thread_seq , a_packet , pstream ) ;
2018-08-22 14:50:00 +08:00
}
return ret ;
}
2018-10-31 20:17:52 +08:00
char kni_close_opstate ( const struct streaminfo * pstream , struct kni_pme_info * pmeinfo , int thread_seq , const void * a_packet , int protocol )
2018-08-22 14:50:00 +08:00
{
char ret = APP_STATE_FAWPKT | APP_STATE_DROPME ;
2018-12-17 08:13:46 +08:00
//add kni_action_redirect 20181216 start
if ( pmeinfo - > action = = KNI_ACTION_REDIRECT )
{
ret = process_redirect_close ( pstream , pmeinfo , thread_seq , a_packet , protocol , pstream - > routedir ) ;
return ret ;
}
//end
2019-01-23 19:49:14 +08:00
if ( a_packet ! = NULL )
2018-08-22 14:50:00 +08:00
{
2019-01-23 19:49:14 +08:00
ret = kni_data_opstate ( pstream , pmeinfo , thread_seq , a_packet , protocol ) ;
2018-08-22 14:50:00 +08:00
}
2018-11-27 19:26:40 +08:00
//del htable
2019-01-25 15:30:53 +06:00
if ( pmeinfo - > action = = KNI_ACTION_MONITOR & & pmeinfo - > is_tcp_repaired = = 1 )
2018-11-27 19:26:40 +08:00
{
2019-01-23 19:49:14 +08:00
kni_htable_del ( pstream , pmeinfo , a_packet ) ;
2018-11-27 19:26:40 +08:00
}
2018-12-17 08:13:46 +08:00
2018-11-27 19:26:40 +08:00
2018-08-22 14:50:00 +08:00
return ret | APP_STATE_DROPME ;
}
2018-10-31 20:17:52 +08:00
extern " C " char kni_udp_entry ( const struct streaminfo * pstream , void * * pme , int thread_seq , const void * a_packet )
{
char ret = APP_STATE_FAWPKT | APP_STATE_DROPME ;
struct kni_ipv6_hdr * ipv6_hdr = NULL ;
2019-01-25 16:39:43 +06:00
struct kni_pme_info * pmeinfo = * ( struct kni_pme_info * * ) pme ;
2018-10-31 20:17:52 +08:00
if ( ( g_kni_switch_info . replace_switch = = 0 ) & & ( g_kni_switch_info . ratelimit_switch = = 0 ) )
{
return APP_STATE_FAWPKT | APP_STATE_DROPME ;
}
if ( pstream - > addr . addrtype = = ADDR_TYPE_IPV6 )
{
ipv6_hdr = ( struct kni_ipv6_hdr * ) a_packet ;
if ( ipv6_hdr - > ip6_nex_hdr ! = NEXTHDR_UDP )
{
return ret ;
}
}
2018-12-10 17:45:19 +08:00
// kni_filestate2_set(thread_seq,FS_UDP,0,1);
2018-10-31 20:17:52 +08:00
switch ( pstream - > opstate )
{
case OP_STATE_PENDING :
2019-01-25 15:30:53 +06:00
* pme = pmeinfo = kni_pmeinfo_new ( ) ;
ret = kni_pending_opstate ( pstream , pmeinfo , thread_seq , a_packet , PROTO_TYPE_UDP ) ;
2018-10-31 20:17:52 +08:00
break ;
case OP_STATE_DATA :
2019-01-25 15:30:53 +06:00
ret = kni_data_opstate ( pstream , pmeinfo , thread_seq , a_packet , PROTO_TYPE_UDP ) ;
2018-10-31 20:17:52 +08:00
break ;
case OP_STATE_CLOSE :
2019-01-25 15:30:53 +06:00
ret = kni_close_opstate ( pstream , pmeinfo , thread_seq , a_packet , PROTO_TYPE_UDP ) ;
2018-10-31 20:17:52 +08:00
break ;
default :
break ;
}
2019-01-25 15:30:53 +06:00
if ( ( ret & APP_STATE_DROPME ) & & pmeinfo ! = NULL )
2018-10-31 20:17:52 +08:00
{
2019-01-25 15:30:53 +06:00
kni_free_pmeinfo ( pmeinfo ) ;
2018-10-31 20:17:52 +08:00
}
2019-01-25 15:30:53 +06:00
* pme = NULL ;
2018-10-31 20:17:52 +08:00
return ret ;
}
extern " C " char kni_tcpall_entry ( const struct streaminfo * pstream , void * * pme , int thread_seq , const void * a_packet )
2018-08-22 14:50:00 +08:00
{
char ret = APP_STATE_FAWPKT | APP_STATE_DROPME ;
2018-09-19 10:26:39 +08:00
struct kni_ipv6_hdr * ipv6_hdr = NULL ;
2019-01-25 16:39:43 +06:00
struct kni_pme_info * pmeinfo = * ( struct kni_pme_info * * ) pme ;
2018-11-22 10:04:34 +08:00
struct timespec start , end ;
long elapse = 0 ;
clock_gettime ( CLOCK_MONOTONIC , & start ) ;
2018-09-19 10:26:39 +08:00
if ( g_kni_comminfo . kni_mode_cur = = KNI_MODE_BYPASS )
2018-08-22 14:50:00 +08:00
{
return ret ;
}
2018-09-19 10:26:39 +08:00
if ( pstream - > addr . addrtype = = ADDR_TYPE_IPV6 )
{
2018-10-31 20:17:52 +08:00
ipv6_hdr = ( struct kni_ipv6_hdr * ) a_packet ;
2018-09-19 10:26:39 +08:00
2018-11-08 18:58:51 +08:00
if ( ( a_packet ! = NULL ) & & ( ipv6_hdr - > ip6_nex_hdr ! = NEXTHDR_TCP ) )
2018-09-19 10:26:39 +08:00
{
2018-11-14 11:31:21 +08:00
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( thread_seq , FS_DROP_IPV6OPT , 0 , 1 ) ;
2018-09-19 10:26:39 +08:00
return ret ;
}
}
2018-08-22 14:50:00 +08:00
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( thread_seq , FS_RX_PKTS , 0 , 1 ) ;
2018-08-22 14:50:00 +08:00
switch ( pstream - > pktstate )
{
case OP_STATE_PENDING :
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( thread_seq , FS_PENDING , 0 , 1 ) ;
kni_filestate2_set ( thread_seq , FS_PMENUM , 0 , 1 ) ;
2019-01-25 15:30:53 +06:00
* pme = pmeinfo = kni_pmeinfo_new ( ) ;
ret = kni_pending_opstate ( pstream , pmeinfo , thread_seq , a_packet , PROTO_TYPE_TCP ) ;
2018-08-22 14:50:00 +08:00
break ;
case OP_STATE_DATA :
2019-01-25 15:30:53 +06:00
ret = kni_data_opstate ( pstream , pmeinfo , thread_seq , a_packet , PROTO_TYPE_TCP ) ;
2018-08-22 14:50:00 +08:00
break ;
case OP_STATE_CLOSE :
2018-11-23 17:13:02 +08:00
if ( a_packet = = NULL )
{
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( thread_seq , FS_CLOSE_TIMEOUT , 0 , 1 ) ;
2018-11-23 17:13:02 +08:00
}
else
{
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( thread_seq , FS_CLOSE_FIN , 0 , 1 ) ;
2018-11-23 17:13:02 +08:00
}
2018-10-31 20:17:52 +08:00
ret = kni_close_opstate ( pstream , ( struct kni_pme_info * ) * pme , thread_seq , a_packet , PROTO_TYPE_TCP ) ;
2018-08-22 14:50:00 +08:00
break ;
default :
break ;
}
2019-01-25 15:30:53 +06:00
if ( ( ret & APP_STATE_DROPME ) & & pmeinfo ! = NULL )
2018-08-22 14:50:00 +08:00
{
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( thread_seq , FS_PMENUM , 0 , - 1 ) ;
2019-01-25 15:30:53 +06:00
kni_free_pmeinfo ( pmeinfo ) ;
2018-08-22 14:50:00 +08:00
* pme = NULL ;
2018-11-27 19:26:40 +08:00
if ( pstream - > pktstate ! = OP_STATE_CLOSE )
{
2018-12-10 17:45:19 +08:00
kni_filestate2_set ( thread_seq , FS_CLOSE_DROPME , 0 , 1 ) ;
2018-11-27 19:26:40 +08:00
}
2018-08-22 14:50:00 +08:00
}
2018-11-22 10:04:34 +08:00
clock_gettime ( CLOCK_MONOTONIC , & end ) ;
elapse = ( end . tv_sec - start . tv_sec ) * 1000000 + ( end . tv_nsec - start . tv_nsec ) / 1000 ;
FS_operate ( g_kni_fs2_info . handler , g_kni_fs2_info . metric_sapp_proc , 0 , FS_OP_SET , elapse ) ;
2018-08-22 14:50:00 +08:00
return ret ;
}
extern " C " char kni_http_entry ( stSessionInfo * session_info , void * * pme , int thread_seq , struct streaminfo * a_stream , const void * a_packet )
{
char ret = PROT_STATE_DROPME ;
2019-01-23 16:57:11 +08:00
http_infor * http_info = ( http_infor * ) ( session_info - > app_info ) ;
if ( ( http_info - > http_session_seq ! = 1 ) | | ( session_info - > prot_flag ! = HTTP_HOST ) )
{
return ret ;
}
2018-08-22 14:50:00 +08:00
2018-12-01 13:38:23 +08:00
int host_len = MIN ( session_info - > buflen , KNI_DEFAULT_MTU ) ;
2018-11-30 18:25:46 +08:00
struct kni_http_project * host_info = ALLOC ( struct kni_http_project , 1 ) ;
2018-08-22 14:50:00 +08:00
host_info - > host_len = host_len ;
memcpy ( host_info - > host , session_info - > buf , host_len ) ;
2019-01-23 16:57:11 +08:00
if ( project_req_add_struct ( a_stream , g_kni_comminfo . project_id , host_info ) < 0 )
{
kni_filestate2_set ( thread_seq , FS_PRO_ERROR , 0 , 1 ) ;
free ( host_info ) ;
host_info = NULL ;
}
2018-08-22 14:50:00 +08:00
return ret ;
}
2018-12-17 10:26:39 +08:00
extern " C " char kni_ipv4_entry ( const struct streaminfo * pstream , unsigned char routedir , int thread_seq , struct ip * ipv4_hdr )
{
if ( ipv4_hdr - > ip_p ! = IPPROTO_ICMP )
{
return APP_STATE_DROPME ;
}
char ret = APP_STATE_GIVEME ;
scan_status_t mid = NULL ;
struct kni_pme_info pmeinfo ;
struct ipaddr addr ;
struct tuple4 ipv4_addr ;
addr . addrtype = ADDR_TYPE_IPV4 ;
addr . paddr = ( void * ) ( & ipv4_addr ) ;
memset ( & ipv4_addr , 0 , sizeof ( ipv4_addr ) ) ;
ipv4_addr . daddr = * ( ( unsigned int * ) & ( ipv4_hdr - > ip_dst ) ) ;
ipv4_addr . saddr = * ( ( unsigned int * ) & ( ipv4_hdr - > ip_src ) ) ;
memset ( & pmeinfo , 0 , sizeof ( pmeinfo ) ) ;
pmeinfo . mid = mid ;
kni_scan_ip ( & addr , thread_seq , ipv4_hdr - > ip_p , & pmeinfo ) ;
Maat_clean_status ( & ( pmeinfo . mid ) ) ;
//add kni_action_redirect 20181216 start
if ( pmeinfo . action = = KNI_ACTION_REDIRECT )
{
ret = process_redirect_pending ( pstream , & pmeinfo , thread_seq , ipv4_hdr , 0 , routedir ) ;
return ret ;
}
2018-12-17 14:48:08 +08:00
else if ( redirect_search_htable ( ADDR_TYPE_IPV4 , & pmeinfo , thread_seq , ipv4_hdr , 0 ) = = 1 )
2018-12-17 10:26:39 +08:00
{
ret = process_redirect_data ( pstream , & pmeinfo , thread_seq , ipv4_hdr , 0 , routedir ) ;
return ret ;
}
//end
return ret ;
}
extern " C " char kni_ipv6_entry ( const struct streaminfo * pstream , unsigned char routedir , int thread_seq , struct kni_ipv6_hdr * ipv6_hdr )
{
if ( ( ipv6_hdr - > ip6_flags [ 0 ] & 0xF0 ) ! = 0x60 )
{
return - 1 ;
}
char ret = APP_STATE_GIVEME ;
scan_status_t mid = NULL ;
struct kni_pme_info pmeinfo ;
struct ipaddr addr ;
struct tuple6 ipv6_addr ;
unsigned char next_hdr_type = ipv6_hdr - > ip6_nex_hdr ;
if ( next_hdr_type ! = IPPROTO_ICMP )
{
return ret ;
}
addr . addrtype = ADDR_TYPE_IPV6 ;
addr . paddr = ( void * ) ( & ipv6_addr ) ;
memset ( & ipv6_addr , 0 , sizeof ( ipv6_addr ) ) ;
memcpy ( ipv6_addr . saddr , & ( ipv6_hdr - > ip6_src ) , sizeof ( ipv6_addr . saddr ) ) ;
memcpy ( ipv6_addr . daddr , & ( ipv6_hdr - > ip6_dst ) , sizeof ( ipv6_addr . saddr ) ) ;
memset ( & pmeinfo , 0 , sizeof ( pmeinfo ) ) ;
pmeinfo . mid = mid ;
kni_scan_ip ( & addr , thread_seq , next_hdr_type , & pmeinfo ) ;
Maat_clean_status ( & ( pmeinfo . mid ) ) ;
//add kni_action_redirect 20181216 start
if ( pmeinfo . action = = KNI_ACTION_REDIRECT )
{
ret = process_redirect_pending ( pstream , & pmeinfo , thread_seq , ipv6_hdr , 0 , routedir ) ;
return ret ;
}
2018-12-17 14:48:08 +08:00
else if ( redirect_search_htable ( ADDR_TYPE_IPV6 , & pmeinfo , thread_seq , ipv6_hdr , 0 ) = = 1 )
2018-12-17 10:26:39 +08:00
{
ret = process_redirect_data ( pstream , & pmeinfo , thread_seq , ipv6_hdr , 0 , routedir ) ;
return ret ;
}
//end
return ret ;
}
/*
2018-10-31 20:17:52 +08:00
extern " C " char kni_ipv4_entry ( const struct streaminfo * pstream , unsigned char routedir , int thread_seq , struct ip * ipv4_hdr )
{
if ( ( ipv4_hdr - > ip_p = = IPPROTO_TCP ) | | ( ipv4_hdr - > ip_p = = IPPROTO_UDP ) | | ( ( g_kni_switch_info . replace_switch = = 0 ) & & ( g_kni_switch_info . ratelimit_switch = = 0 ) ) )
{
return APP_STATE_DROPME ;
}
2018-12-10 17:45:19 +08:00
// kni_filestate2_set(thread_seq,FS_IP,0,1);
2018-11-26 09:10:54 +08:00
2018-10-31 20:17:52 +08:00
char ret = APP_STATE_GIVEME ;
scan_status_t mid = NULL ;
struct kni_pme_info pmeinfo ;
2018-12-17 10:26:39 +08:00
// int payload_len = ntohs(ipv4_hdr->ip_len) - 4*(ipv4_hdr->ip_hl);
// char* payload = (char*)ipv4_hdr + 4*(ipv4_hdr->ip_hl);
2018-10-31 20:17:52 +08:00
struct ipaddr addr ;
struct tuple4 ipv4_addr ;
addr . addrtype = ADDR_TYPE_IPV4 ;
addr . paddr = ( void * ) ( & ipv4_addr ) ;
2018-12-01 15:34:55 +08:00
memset ( & ipv4_addr , 0 , sizeof ( ipv4_addr ) ) ;
2018-10-31 20:17:52 +08:00
ipv4_addr . daddr = * ( ( unsigned int * ) & ( ipv4_hdr - > ip_dst ) ) ;
ipv4_addr . saddr = * ( ( unsigned int * ) & ( ipv4_hdr - > ip_src ) ) ;
2018-12-01 15:34:55 +08:00
memset ( & pmeinfo , 0 , sizeof ( pmeinfo ) ) ;
2018-10-31 20:17:52 +08:00
pmeinfo . mid = mid ;
kni_scan_ip ( & addr , thread_seq , ipv4_hdr - > ip_p , & pmeinfo ) ;
// kni_scan_pktbin(payload,payload_len,thread_seq,&pmeinfo);
Maat_clean_status ( & ( pmeinfo . mid ) ) ;
2018-12-11 23:16:41 +08:00
ret = kni_process_udppkt ( routedir , & pmeinfo , thread_seq , ipv4_hdr , pstream ) ;
2018-10-31 20:17:52 +08:00
return ret ;
}
extern " C " char kni_ipv6_entry ( const struct streaminfo * pstream , unsigned char routedir , int thread_seq , struct kni_ipv6_hdr * ipv6_hdr )
{
if ( ( ipv6_hdr - > ip6_flags [ 0 ] & 0xF0 ) ! = 0x60 )
{
return - 1 ;
}
char ret = APP_STATE_GIVEME ;
scan_status_t mid = NULL ;
struct kni_pme_info pmeinfo ;
struct ipaddr addr ;
struct tuple6 ipv6_addr ;
unsigned char next_hdr_type = ipv6_hdr - > ip6_nex_hdr ;
if ( ( next_hdr_type = = PROTO_TYPE_TCP ) | | ( next_hdr_type = = PROTO_TYPE_UDP ) )
{
return ret ;
}
2018-11-26 09:10:54 +08:00
2018-12-10 17:45:19 +08:00
// kni_filestate2_set(thread_seq,FS_IP,0,1);
2018-10-31 20:17:52 +08:00
addr . addrtype = ADDR_TYPE_IPV6 ;
addr . paddr = ( void * ) ( & ipv6_addr ) ;
2018-12-01 15:34:55 +08:00
memset ( & ipv6_addr , 0 , sizeof ( ipv6_addr ) ) ;
2018-12-01 17:51:39 +08:00
memcpy ( ipv6_addr . saddr , & ( ipv6_hdr - > ip6_src ) , sizeof ( ipv6_addr . saddr ) ) ;
memcpy ( ipv6_addr . daddr , & ( ipv6_hdr - > ip6_dst ) , sizeof ( ipv6_addr . saddr ) ) ;
// ipv6_addr.saddr=ipv6_hdr->ip6_src.s6_addr32;
// ipv6_addr.daddr=ipv6_hdr->ip6_dst.s6_addr32;
2018-10-31 20:17:52 +08:00
2018-12-01 15:34:55 +08:00
memset ( & pmeinfo , 0 , sizeof ( pmeinfo ) ) ;
2018-10-31 20:17:52 +08:00
pmeinfo . mid = mid ;
kni_scan_ip ( & addr , thread_seq , next_hdr_type , & pmeinfo ) ;
Maat_clean_status ( & ( pmeinfo . mid ) ) ;
2018-12-11 23:16:41 +08:00
ret = kni_process_udppkt ( routedir , & pmeinfo , thread_seq , ipv6_hdr , pstream ) ;
2018-10-31 20:17:52 +08:00
return ret ;
}
2018-12-17 10:26:39 +08:00
*/
2018-10-31 20:17:52 +08:00
2018-08-22 14:50:00 +08:00
void kni_free_project ( int thread_seq , void * project_req_value )
{
free ( project_req_value ) ;
project_req_value = NULL ;
return ;
}
int kni_read_cardname ( )
{
int offset = 0 ;
char * token = NULL ;
char buf [ KNI_CONF_MAXLEN ] = { 0 } ;
int routdir = 0 ;
FILE * fp = fopen ( " ./conf/send_raw_pkt.conf " , " r " ) ;
if ( fp = = NULL )
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , " kni_read_cardname " , " fopen ./conf/send_raw_pkt.conf err,errno:%d,%s " , errno , strerror ( errno ) ) ;
return - 1 ;
}
while ( feof ( fp ) = = 0 )
{
routdir = 0 ;
2018-11-30 18:25:46 +08:00
memset ( buf , 0 , sizeof ( buf ) ) ;
2018-08-22 14:50:00 +08:00
if ( ( fgets ( buf , KNI_CONF_MAXLEN , fp ) = = NULL ) )
{
break ;
}
if ( buf [ 0 ] = = ' # ' )
{
continue ;
}
token = strtok ( buf , " \t , " ) ;
offset = 1 ;
while ( token ! = NULL )
{
switch ( offset )
{
case KNI_OFFSET_ROUTDIR :
routdir = atoi ( token ) ;
if ( ( routdir ! = 0 ) & & ( routdir ! = 1 ) )
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , " kni_read_cardname " , " routdir :%d error " , routdir ) ;
return - 1 ;
}
break ;
case KNI_OFFSET_CARDNAME :
memcpy ( g_kni_cardname [ routdir ] , token , strlen ( token ) ) ;
2018-11-30 18:25:46 +08:00
assert ( strlen ( token ) < KNI_CONF_MAXLEN ) ;
2018-08-22 14:50:00 +08:00
break ;
default :
break ;
}
token = strtok ( NULL , " \t , " ) ;
offset + + ;
}
}
fclose ( fp ) ;
fp = NULL ;
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , " kni_read_cardname " , " card name:%s,%s " , g_kni_cardname [ 0 ] , g_kni_cardname [ 1 ] ) ;
return 0 ;
}
int init_profile_info ( )
{
//main.conf
// MESA_load_profile_int_def((char*)KNI_CONF_FILENAME_MAIN,(char*)KNI_CONF_MODE,(char*)"thread_num",&(g_kni_comminfo.thread_num),1);
MESA_load_profile_string_def ( ( char * ) KNI_CONF_FILENAME_MAIN , ( char * ) KNI_CONF_MODE , ( char * ) " pcapdevice " , g_kni_comminfo . card_in , KNI_CONF_MAXLEN , " cap0 " ) ;
MESA_load_profile_string_def ( ( char * ) KNI_CONF_FILENAME_MAIN , ( char * ) KNI_CONF_MODE , ( char * ) " pcapdevice2 " , g_kni_comminfo . card_out , KNI_CONF_MAXLEN , " cap1 " ) ;
//kni.conf
2018-11-30 17:25:48 +08:00
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_MAIN_MODE , ( char * ) " replay_win_update " , & ( g_kni_switch_info . replay_win_update ) , 1 ) ;
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_MAIN_MODE , ( char * ) " default_work_mode " , & ( g_kni_switch_info . maat_default_mode ) , KNI_DEFAULT_MODE_INTERCEPT ) ;
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_MAIN_MODE , ( char * ) " ratelimit_switch " , & ( g_kni_switch_info . ratelimit_switch ) , 1 ) ;
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_MAIN_MODE , ( char * ) " replace_switch " , & ( g_kni_switch_info . replace_switch ) , 1 ) ;
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_MAIN_MODE , ( char * ) " sendpkt_mode " , & ( g_kni_switch_info . sendpkt_mode ) , 0 ) ;
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_MAIN_MODE , ( char * ) " write_listqueue_switch " , & ( g_kni_switch_info . write_listq_switch ) , 0 ) ;
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_MAIN_MODE , ( char * ) " send_fds_mode " , & ( g_kni_switch_info . send_fds_mode ) , 0 ) ;
MESA_load_profile_string_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_MAIN_MODE , ( char * ) " domain_path " , g_kni_comminfo . domain_path , KNI_CONF_MAXLEN , " /home/server_unixsocket_file " ) ;
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_MAIN_MODE , ( char * ) " socketopt_mark " , & ( g_kni_comminfo . mark ) , 101 ) ;
2018-11-27 19:26:40 +08:00
2018-08-22 14:50:00 +08:00
return 0 ;
}
int init_kni_stat_htable ( )
{
2018-12-28 13:33:55 +08:00
int htable_elem_num = 0 ;
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_MAIN_MODE , ( char * ) " htable_elem_num " , & htable_elem_num , KNI_HTABLE_MAXNUM ) ;
2018-08-22 14:50:00 +08:00
MESA_htable_create_args_t hash_frags ;
2018-11-30 18:25:46 +08:00
memset ( & hash_frags , 0 , sizeof ( hash_frags ) ) ;
2018-08-22 14:50:00 +08:00
hash_frags . thread_safe = KNI_THREAD_SAFE ;
hash_frags . recursive = 1 ;
hash_frags . hash_slot_size = KNI_HTABLE_SIZE ;
2018-12-28 13:33:55 +08:00
hash_frags . max_elem_num = htable_elem_num ;
2018-08-22 14:50:00 +08:00
hash_frags . eliminate_type = HASH_ELIMINATE_ALGO_FIFO ;
hash_frags . expire_time = 0 ;
hash_frags . key_comp = NULL ;
hash_frags . key2index = NULL ;
hash_frags . data_free = NULL ;
hash_frags . data_expire_with_condition = NULL ;
g_kni_structinfo . htable_to_tun_v4 = MESA_htable_create ( & hash_frags , sizeof ( MESA_htable_create_args_t ) ) ;
g_kni_structinfo . htable_to_tun_v6 = MESA_htable_create ( & hash_frags , sizeof ( MESA_htable_create_args_t ) ) ;
if ( ( g_kni_structinfo . htable_to_tun_v4 = = NULL ) | | ( g_kni_structinfo . htable_to_tun_v6 = = NULL ) )
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " MESA_htable_create() error!action:%s " , KNI_ACTION_EXIT ) ;
return - 1 ;
}
return 0 ;
}
int init_kni_runtimelog ( )
{
2018-12-10 17:45:19 +08:00
// int logger_level;
2018-08-22 14:50:00 +08:00
char logger_filepath [ KNI_CONF_MAXLEN ] = { 0 } ;
2018-12-10 17:45:19 +08:00
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_MAIN_MODE , ( char * ) " logger_level " , & ( g_kni_comminfo . logger_level ) , RLOG_LV_INFO ) ;
2018-11-30 17:25:48 +08:00
MESA_load_profile_string_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_MAIN_MODE , ( char * ) " logger_filepath " , logger_filepath , KNI_CONF_MAXLEN , " ./log/kni.log " ) ;
2018-08-22 14:50:00 +08:00
2018-12-10 17:45:19 +08:00
g_kni_comminfo . logger = MESA_create_runtime_log_handle ( logger_filepath , g_kni_comminfo . logger_level ) ;
2018-08-22 14:50:00 +08:00
if ( g_kni_comminfo . logger = = NULL )
{
printf ( " MESA_create_runtime_log_handle() error!exit... \n " ) ;
return - 1 ;
}
return 0 ;
}
2018-11-07 17:33:02 +08:00
int init_kni_dyn_maat_info ( )
2018-08-22 14:50:00 +08:00
{
int ret = 0 ;
int maat_readconf_mode = 0 ;
2018-11-07 17:33:02 +08:00
char redis_ip [ INET_ADDRSTRLEN ] = { 0 } ;
2018-08-22 14:50:00 +08:00
int redis_db_index = 0 ;
2018-12-04 22:29:50 +08:00
int redis_port_begin = 0 , redis_port_end = 0 ;
unsigned short redis_port_select = 0 ;
char redis_port_range [ KNI_CONF_MAXLEN ] = { 0 } ;
2018-11-07 17:33:02 +08:00
int scandir_interval = KNI_SCANDIR_INTERVAL ;
int effect_interval = KNI_EFFECT_INTERVAL ;
char table_info_path [ KNI_CONF_MAXLEN ] = { 0 } ;
char stat_file_dir [ KNI_CONF_MAXLEN ] = { 0 } ;
2018-11-30 17:25:48 +08:00
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_DYNMAAT_MODE , ( char * ) " dyn_maat_readconf_mode " , & maat_readconf_mode , KNI_READCONF_IRIS ) ;
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_DYNMAAT_MODE , ( char * ) " dyn_scandir_interval " , & scandir_interval , KNI_SCANDIR_INTERVAL ) ;
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_DYNMAAT_MODE , ( char * ) " dyn_effect_interval " , & effect_interval , KNI_EFFECT_INTERVAL ) ;
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_DYNMAAT_MODE , ( char * ) " dyn_redis_db_index " , & redis_db_index , 0 ) ;
2018-12-04 22:29:50 +08:00
// MESA_load_profile_int_nodef((char*)KNI_CONF_FILENAME,(char*)KNI_DYNMAAT_MODE,(char*)"dyn_redis_port",(int*)&redis_port);
2018-11-07 17:33:02 +08:00
2018-11-30 17:25:48 +08:00
MESA_load_profile_string_nodef ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_DYNMAAT_MODE , ( char * ) " dyn_redis_server " , redis_ip , INET_ADDRSTRLEN ) ;
2018-12-04 22:29:50 +08:00
MESA_load_profile_string_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_DYNMAAT_MODE , ( char * ) " dyn_redis_port " , redis_port_range , sizeof ( redis_port_range ) , " 6379 " ) ;
2018-11-30 17:25:48 +08:00
MESA_load_profile_string_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_DYNMAAT_MODE , ( char * ) " dyn_stat_file_path " , stat_file_dir , KNI_CONF_MAXLEN , KNI_DYN_STAT_FILEPATH ) ;
MESA_load_profile_string_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_DYNMAAT_MODE , ( char * ) " dyn_table_info_path " , table_info_path , KNI_CONF_MAXLEN , KNI_TABLEINFO_PATH ) ;
2018-11-07 17:33:02 +08:00
g_kni_maatinfo . ipd_dyn_maat_feather = Maat_feather ( g_iThreadNum , table_info_path , g_kni_comminfo . logger ) ;
if ( g_kni_maatinfo . ipd_dyn_maat_feather = = NULL )
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " dyn_Maat_feather() error!table_info_path:%s,action:%s " , table_info_path , KNI_ACTION_EXIT ) ;
return - 1 ;
}
if ( maat_readconf_mode = = KNI_READCONF_JSON )
{
Maat_set_feather_opt ( g_kni_maatinfo . ipd_dyn_maat_feather , MAAT_OPT_JSON_FILE_PATH , KNI_MAATJSON_FILEPATH , strlen ( KNI_MAATJSON_FILEPATH ) ) ;
}
else if ( maat_readconf_mode = = KNI_READCONF_REDIS )
{
2018-12-04 22:29:50 +08:00
ret = sscanf ( redis_port_range , " %d-%d " , & redis_port_begin , & redis_port_end ) ;
if ( ret = = 1 )
{
redis_port_select = ( unsigned short ) redis_port_begin ;
}
else if ( ret = = 2 )
{
srand ( time ( NULL ) ) ;
redis_port_select = ( unsigned short ) ( redis_port_begin + rand ( ) % ( redis_port_end - redis_port_begin ) ) ;
}
else
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " Invalid redis port range %s, MAAT init failed. " , redis_port_range ) ;
return - 1 ;
}
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " dynmic_maat_redis select port:%d " , redis_port_select ) ;
2018-11-07 17:33:02 +08:00
Maat_set_feather_opt ( g_kni_maatinfo . ipd_dyn_maat_feather , MAAT_OPT_REDIS_IP , ( void * ) redis_ip , strlen ( redis_ip ) + 1 ) ;
2018-12-04 22:29:50 +08:00
Maat_set_feather_opt ( g_kni_maatinfo . ipd_dyn_maat_feather , MAAT_OPT_REDIS_PORT , ( void * ) & redis_port_select , sizeof ( unsigned short ) ) ;
2018-11-07 17:33:02 +08:00
Maat_set_feather_opt ( g_kni_maatinfo . ipd_dyn_maat_feather , MAAT_OPT_REDIS_INDEX , ( void * ) & redis_db_index , sizeof ( int ) ) ;
}
Maat_set_feather_opt ( g_kni_maatinfo . ipd_dyn_maat_feather , MAAT_OPT_SCANDIR_INTERVAL_MS , ( void * ) & scandir_interval , sizeof ( int ) ) ;
Maat_set_feather_opt ( g_kni_maatinfo . ipd_dyn_maat_feather , MAAT_OPT_EFFECT_INVERVAL_MS , ( void * ) & effect_interval , sizeof ( int ) ) ;
Maat_set_feather_opt ( g_kni_maatinfo . ipd_dyn_maat_feather , MAAT_OPT_STAT_FILE_PATH , stat_file_dir , strlen ( stat_file_dir ) ) ;
ret = Maat_initiate_feather ( g_kni_maatinfo . ipd_dyn_maat_feather ) ;
if ( ret < 0 )
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " Maat_initiate_feather() error!action:%s " , KNI_ACTION_EXIT ) ;
return - 1 ;
}
g_kni_maatinfo . tableid_dynamic_domain = Maat_table_register ( g_kni_maatinfo . ipd_dyn_maat_feather , KNI_TABLENAME_DNY_DOMAIN ) ;
if ( g_kni_maatinfo . tableid_dynamic_domain < 0 )
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " dyn_Maat_table_register() error!tableid_dynamic_domain:%d,action:%s " , g_kni_maatinfo . tableid_dynamic_domain , KNI_ACTION_EXIT ) ;
return - 1 ;
}
return 0 ;
}
2018-11-26 09:10:54 +08:00
int init_kni_static_maat_info ( )
2018-11-07 17:33:02 +08:00
{
int ret = 0 ;
int maat_readconf_mode = 0 ;
2018-08-22 14:50:00 +08:00
char redis_ip [ INET_ADDRSTRLEN ] = { 0 } ;
2018-11-07 17:33:02 +08:00
int redis_db_index = 0 ;
2018-12-04 22:29:50 +08:00
int redis_port_begin = 0 , redis_port_end = 0 ;
unsigned short redis_port_select = 0 ;
2018-11-07 17:33:02 +08:00
2018-08-22 14:50:00 +08:00
int scandir_interval = KNI_SCANDIR_INTERVAL ;
int effect_interval = KNI_EFFECT_INTERVAL ;
char table_info_path [ KNI_CONF_MAXLEN ] = { 0 } ;
char stat_file_dir [ KNI_CONF_MAXLEN ] = { 0 } ;
char full_cfg_dir [ KNI_CONF_MAXLEN ] = { 0 } ;
char inc_cfg_dir [ KNI_CONF_MAXLEN ] = { 0 } ;
2018-12-04 22:29:50 +08:00
char redis_port_range [ KNI_CONF_MAXLEN ] = { 0 } ;
2018-08-22 14:50:00 +08:00
2018-12-01 13:38:23 +08:00
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_STATIC_MAAT_MODE , ( char * ) " maat_readconf_mode " , & maat_readconf_mode , KNI_READCONF_IRIS ) ;
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_STATIC_MAAT_MODE , ( char * ) " scandir_interval " , & scandir_interval , KNI_SCANDIR_INTERVAL ) ;
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_STATIC_MAAT_MODE , ( char * ) " effect_interval " , & effect_interval , KNI_EFFECT_INTERVAL ) ;
MESA_load_profile_int_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_STATIC_MAAT_MODE , ( char * ) " redis_db_index " , & redis_db_index , 0 ) ;
2018-12-04 22:29:50 +08:00
// MESA_load_profile_int_nodef((char*)KNI_CONF_FILENAME,(char*)KNI_STATIC_MAAT_MODE,(char*)"redis_port",(int*)&redis_port);
2018-12-01 13:38:23 +08:00
2018-12-04 22:29:50 +08:00
MESA_load_profile_string_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_STATIC_MAAT_MODE , ( char * ) " redis_port " , redis_port_range , sizeof ( redis_port_range ) , " 6379 " ) ;
2018-12-01 13:38:23 +08:00
MESA_load_profile_string_nodef ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_STATIC_MAAT_MODE , ( char * ) " redis_server " , redis_ip , INET_ADDRSTRLEN ) ;
MESA_load_profile_string_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_STATIC_MAAT_MODE , ( char * ) " table_info_path " , table_info_path , KNI_CONF_MAXLEN , KNI_TABLEINFO_PATH ) ;
MESA_load_profile_string_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_STATIC_MAAT_MODE , ( char * ) " inc_cfg_dir " , inc_cfg_dir , KNI_CONF_MAXLEN , KNI_INCCFG_FILEPATH ) ;
MESA_load_profile_string_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_STATIC_MAAT_MODE , ( char * ) " stat_file_path " , stat_file_dir , KNI_CONF_MAXLEN , KNI_STAT_FILEPATH ) ;
MESA_load_profile_string_def ( ( char * ) KNI_CONF_FILENAME , ( char * ) KNI_STATIC_MAAT_MODE , ( char * ) " full_cfg_dir " , full_cfg_dir , KNI_CONF_MAXLEN , KNI_FULLCFG_FILEPATH ) ;
2018-08-22 14:50:00 +08:00
2018-12-04 22:29:50 +08:00
2018-08-22 14:50:00 +08:00
g_kni_maatinfo . maat_feather = Maat_feather ( g_iThreadNum , table_info_path , g_kni_comminfo . logger ) ;
2018-11-07 17:33:02 +08:00
if ( g_kni_maatinfo . maat_feather = = NULL )
2018-08-22 14:50:00 +08:00
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " Maat_feather() error!table_info_path:%s,action:%s " , table_info_path , KNI_ACTION_EXIT ) ;
return - 1 ;
}
if ( maat_readconf_mode = = KNI_READCONF_JSON )
{
Maat_set_feather_opt ( g_kni_maatinfo . maat_feather , MAAT_OPT_JSON_FILE_PATH , KNI_MAATJSON_FILEPATH , strlen ( KNI_MAATJSON_FILEPATH ) ) ;
}
else if ( maat_readconf_mode = = KNI_READCONF_IRIS )
{
Maat_set_feather_opt ( g_kni_maatinfo . maat_feather , MAAT_OPT_FULL_CFG_DIR , full_cfg_dir , strlen ( full_cfg_dir ) ) ;
Maat_set_feather_opt ( g_kni_maatinfo . maat_feather , MAAT_OPT_INC_CFG_DIR , inc_cfg_dir , strlen ( inc_cfg_dir ) ) ;
}
else if ( maat_readconf_mode = = KNI_READCONF_REDIS )
{
2018-12-04 22:29:50 +08:00
ret = sscanf ( redis_port_range , " %d-%d " , & redis_port_begin , & redis_port_end ) ;
if ( ret = = 1 )
{
redis_port_select = ( unsigned short ) redis_port_begin ;
}
else if ( ret = = 2 )
{
srand ( time ( NULL ) ) ;
redis_port_select = ( unsigned short ) ( redis_port_begin + rand ( ) % ( redis_port_end - redis_port_begin ) ) ;
}
else
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " Invalid redis port range %s, MAAT init failed. " , redis_port_range ) ;
return - 1 ;
}
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " static_maat_redis select port:%d " , redis_port_select ) ;
2018-08-22 14:50:00 +08:00
Maat_set_feather_opt ( g_kni_maatinfo . maat_feather , MAAT_OPT_REDIS_IP , ( void * ) redis_ip , strlen ( redis_ip ) + 1 ) ;
2018-12-04 22:29:50 +08:00
Maat_set_feather_opt ( g_kni_maatinfo . maat_feather , MAAT_OPT_REDIS_PORT , ( void * ) & redis_port_select , sizeof ( unsigned short ) ) ;
2018-08-22 14:50:00 +08:00
Maat_set_feather_opt ( g_kni_maatinfo . maat_feather , MAAT_OPT_REDIS_INDEX , ( void * ) & redis_db_index , sizeof ( int ) ) ;
}
Maat_set_feather_opt ( g_kni_maatinfo . maat_feather , MAAT_OPT_SCANDIR_INTERVAL_MS , ( void * ) & scandir_interval , sizeof ( int ) ) ;
Maat_set_feather_opt ( g_kni_maatinfo . maat_feather , MAAT_OPT_EFFECT_INVERVAL_MS , ( void * ) & effect_interval , sizeof ( int ) ) ;
Maat_set_feather_opt ( g_kni_maatinfo . maat_feather , MAAT_OPT_STAT_FILE_PATH , stat_file_dir , strlen ( stat_file_dir ) ) ;
ret = Maat_initiate_feather ( g_kni_maatinfo . maat_feather ) ;
if ( ret < 0 )
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " Maat_initiate_feather() error!action:%s " , KNI_ACTION_EXIT ) ;
return - 1 ;
}
g_kni_maatinfo . tableid_ip = Maat_table_register ( g_kni_maatinfo . maat_feather , KNI_TABLENAME_IP ) ;
g_kni_maatinfo . tableid_domain = Maat_table_register ( g_kni_maatinfo . maat_feather , KNI_TABLENAME_DOMAIN ) ;
2018-10-31 20:17:52 +08:00
g_kni_maatinfo . tableid_pktbin = Maat_table_register ( g_kni_maatinfo . maat_feather , KNI_TABLENAME_PKTBIN ) ;
2018-12-17 08:13:46 +08:00
g_kni_maatinfo . tableid_spoofing_ip = Maat_table_register ( g_kni_maatinfo . maat_feather , KNI_TABLENAME_SPOOFING_IP ) ;
if ( ( g_kni_maatinfo . tableid_ip < 0 ) | | ( g_kni_maatinfo . tableid_domain < 0 ) | | ( g_kni_maatinfo . tableid_pktbin < 0 ) | | ( g_kni_maatinfo . tableid_spoofing_ip < 0 ) )
2018-08-22 14:50:00 +08:00
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " Maat_table_register() error!ip_tableid:%d,sni_tableid:%d,action:%s " , g_kni_maatinfo . tableid_ip , g_kni_maatinfo . tableid_domain , KNI_ACTION_EXIT ) ;
return - 1 ;
}
2018-12-17 08:13:46 +08:00
ret = Maat_plugin_EX_register ( g_kni_maatinfo . maat_feather , g_kni_maatinfo . tableid_spoofing_ip ,
plugin_EX_new_cb ,
plugin_EX_free_cb ,
plugin_EX_dup_cb ,
NULL ,
0 , NULL ) ;
if ( ret < 0 )
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " Maat_plugin_EX_register() error! " ) ;
return - 1 ;
}
2018-08-22 14:50:00 +08:00
return 0 ;
}
int init_kni_project ( )
{
g_kni_comminfo . project_id = project_producer_register ( KNI_PROJECT_NAME , PROJECT_VAL_TYPE_STRUCT , kni_free_project ) ;
if ( g_kni_comminfo . project_id < 0 )
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " project_producer_register() error!project_id:%d " , g_kni_comminfo . project_id ) ;
return - 1 ;
}
g_kni_comminfo . project_id = project_customer_register ( KNI_PROJECT_NAME , PROJECT_VAL_TYPE_STRUCT ) ;
if ( g_kni_comminfo . project_id < 0 )
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " project_customer_register() error!project_id:%d " , g_kni_comminfo . project_id ) ;
return - 1 ;
}
return 0 ;
}
2018-11-30 17:25:48 +08:00
int init_kni_tunprocess ( )
{
int i = 0 ;
2018-08-22 14:50:00 +08:00
2018-11-30 17:25:48 +08:00
pthread_t pid_read_tun ;
for ( i = 0 ; i < g_kni_comminfo . tun_threadnum ; i + + )
{
g_kni_threadseq [ i ] = i ;
pthread_create ( & pid_read_tun , NULL , pthread_process_tun , & ( g_kni_threadseq [ i ] ) ) ;
}
return 0 ;
}
2018-08-22 14:50:00 +08:00
int init_kni_sendpkt ( )
{
int i = 0 ;
kni_read_cardname ( ) ;
2018-12-01 13:38:23 +08:00
g_kni_comminfo . fd_sendpkt = ALLOC ( int , g_kni_comminfo . thread_num ) ;
2018-08-22 14:50:00 +08:00
for ( i = 0 ; i < g_kni_comminfo . thread_num ; i + + )
{
2018-11-08 18:58:51 +08:00
// g_kni_comminfo.ipv4_fd[i]=socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
2018-11-26 09:10:54 +08:00
g_kni_comminfo . fd_sendpkt [ i ] = socket ( AF_PACKET , SOCK_RAW , htons ( ETH_P_IP ) ) ;
if ( g_kni_comminfo . fd_sendpkt [ i ] < 0 )
2018-08-22 14:50:00 +08:00
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " ipv4_raw_socket error,i:%d,action:%s " , i , KNI_ACTION_EXIT ) ;
return - 1 ;
}
}
return 0 ;
}
2018-11-26 09:10:54 +08:00
int init_kni_lqueue ( )
2018-11-23 20:41:03 +08:00
{
int i = 0 ;
g_kni_structinfo . lqueue_send_fds = MESA_lqueue_create ( KNI_THREAD_SAFE , KNI_LQUEUE_MAXNUM ) ;
if ( g_kni_structinfo . lqueue_send_fds = = NULL )
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " MESA_lqueue_create() error for lqueue_send_fds,action:%s " , KNI_ACTION_EXIT ) ;
return - 1 ;
}
2018-11-30 17:25:48 +08:00
for ( i = 0 ; i < g_kni_comminfo . tun_threadnum ; i + + )
2018-11-23 20:41:03 +08:00
{
2018-11-26 09:10:54 +08:00
g_kni_structinfo . lqueue_write_tun [ i ] = MESA_lqueue_create ( KNI_THREAD_SAFE , KNI_LQUEUE_MAXNUM ) ;
2018-11-23 20:41:03 +08:00
if ( g_kni_structinfo . lqueue_write_tun [ i ] = = NULL )
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " MESA_lqueue_create() error for lqueue_write_tun,thread_num:%d,action:%s " , i , KNI_ACTION_EXIT ) ;
return - 1 ;
}
}
return 0 ;
}
2018-08-22 14:50:00 +08:00
extern " C " char kni_init ( )
{
2018-11-26 09:10:54 +08:00
2018-08-22 14:50:00 +08:00
pthread_t pid_kni_filestat2 ;
2018-11-30 17:25:48 +08:00
g_kni_comminfo . tun_threadnum = g_iThreadNum ;
2018-08-22 14:50:00 +08:00
init_profile_info ( ) ;
2018-11-26 09:10:54 +08:00
if ( init_kni_runtimelog ( ) < 0 )
2018-08-22 14:50:00 +08:00
{
return - 1 ;
}
2018-11-26 09:10:54 +08:00
if ( init_kni_project ( ) < 0 )
2018-08-22 14:50:00 +08:00
{
return - 1 ;
}
2018-11-26 09:10:54 +08:00
if ( init_kni_static_maat_info ( ) < 0 )
2018-08-22 14:50:00 +08:00
{
return - 1 ;
}
2018-11-07 17:33:02 +08:00
2018-11-26 09:10:54 +08:00
if ( init_kni_dyn_maat_info ( ) < 0 )
2018-11-07 17:33:02 +08:00
{
return - 1 ;
}
2018-11-26 09:10:54 +08:00
2018-08-22 14:50:00 +08:00
2018-11-26 09:10:54 +08:00
if ( init_kni_stat_htable ( ) < 0 )
2018-08-22 14:50:00 +08:00
{
return - 1 ;
}
2018-11-26 09:10:54 +08:00
if ( init_kni_lqueue ( ) < 0 )
2018-08-22 14:50:00 +08:00
{
return - 1 ;
}
2018-11-26 09:10:54 +08:00
2018-12-17 08:13:46 +08:00
kni_init_redirect_htable ( ) ;
2018-11-26 09:10:54 +08:00
if ( init_kni_tun ( ) < 0 )
2018-08-22 14:50:00 +08:00
{
return - 1 ;
}
2018-11-26 09:10:54 +08:00
init_kni_unixdomain ( ) ;
2018-11-27 19:26:40 +08:00
pthread_create ( & pid_kni_filestat2 , NULL , kni_filestat2 , NULL ) ;
2018-08-22 14:50:00 +08:00
2018-11-26 09:10:54 +08:00
if ( init_kni_sendpkt ( ) < 0 )
2018-08-22 14:50:00 +08:00
{
return - 1 ;
}
2018-11-30 17:25:48 +08:00
init_kni_tunprocess ( ) ;
2018-12-29 18:08:23 +06:00
kni_sendlog_init ( ) ;
2018-08-22 14:50:00 +08:00
if ( g_kni_switch_info . maat_default_mode = = 0 )
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " default work module is intercept " ) ;
}
else if ( g_kni_switch_info . maat_default_mode = = 1 )
{
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " default work module is bypass " ) ;
}
MESA_handle_runtime_log ( g_kni_comminfo . logger , RLOG_LV_FATAL , KNI_MODULE_INIT , " kni init succ! " ) ;
return 0 ;
}