summaryrefslogtreecommitdiff
path: root/include/MESA/stream_inc/stream_inject.h
blob: 68259f06351b6b642e35538330f29a49e1845adf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#ifndef _APP_STREAM_INJECT_H_
#define _APP_STREAM_INJECT_H_ 

#include <sys/types.h>
#include <stdint.h>
#include "stream_base.h"

#ifdef __cplusplus
extern "C" {
#endif

#define STREAM_INJECT_H_VERSION		(20191120)


/* 
	CHN : ����GK��غ��� 
	ENG : to force terminate a stream;

	MESA_kill_tcp: use RST to terminate a TCP stream;
	MESA_kill_tcp_synack: send phony SYN/ACK packet to cheat client and server.
	MESA_kill_connection: for non-TCP stream, such as UDP stream, only available in serial mode.

    return value:
		>= 0: success.
		-1  : error.
*/
int MESA_kill_tcp(struct streaminfo *stream, const void *raw_pkt);
int MESA_kill_tcp_synack(struct streaminfo *stream, const void *raw_pkt);
int MESA_kill_connection(struct streaminfo *stream, const void *ext_raw_pkt);

struct rst_tcp_para{
	unsigned char  th_flags; /* TCPͷ����־λ, ��ѡֵΪ[TH_RST, TH_RST|TH_ACK] */
	unsigned char  rst_pkt_num;  /* ����һ��MESA_rst_tcp()���͵�rst������, ��ѡֵ[1,2,3], ��Ե�������, ��rst_pkt_num=2, dir=DIR_DOUBLEʱ,�ܷ���������4��  */
	/*  
	    dir:
	    ����rst������, ��ѡֵΪ[DIR_C2S, DIR_S2C, DIR_DOUBLE], ��ֵ�ο�:streaminfo->curdir,  
		��������͵İ��뵱ǰ��ͬ��, dir = stream->curdir, 
		��������͵İ��뵱ǰ������, dir = stream->curdir ^ 3, ������.
		�����˫����, dir = DIR_DOUBLE;
	*/
	unsigned char  dir; 
	unsigned char __pad_no_use; /* padding for alignment */
	
	/* 
	   rst��ָ����Ϣ, �Ƽ�ֵseed1=65535, seed2Ϊ����, ��13,17,19��; 
	   signature_seed1=0 && signature_seed1=0, ��ʾ���ε��ò�ָ��signature, ʹ��ȫ������, ��������:
	   if(sapp.toml->stream.tcp.inject.signature_enabled == 1){
			signature_seed1 = sapp.toml->stream.tcp.inject.signature_seed1;
			signature_seed2 = sapp.toml->stream.tcp.inject.signature_seed2;
	   }else{
		   signature_seed1 = rand();
		   signature_seed2 = rand();
	   }
	*/
	unsigned short signature_seed1; 
	unsigned short signature_seed2; 
};

/*
	args:
		stream: ��ǰ��������;
		paras : ����rst��ز���, ���struct rst_tcp_para�ṹ��˵��;
		para_len: sizeof(struct rst_tcp_para), Ԥ��������������, ���ݴ˳����жϰ汾.
		
	MESA_rst_tcp��MESA_kill_tcp����:
		MESA_kill_tcpʵ�����Ǽ��������Ĵ��ӻ�: ����RST��, ���Ʒ�����APP_STATE_KILL_OTHER, ��DROP��ǰ���������а�;
		MESA_rst_tcpֻר����һ������: ����RST��!

	note: ������IJ���֮��, kill_tcp����һ��������FD����, 
	      ��������Ϊ��������Ŵ���������ʱ��ԭ��, ���µ���FD����Ч, ����MESA_kill_tcp֮������Զ�����FD����, 
	      ������MESA_rst_tcp��˵, ������Ϊֻ�Ե�ǰ����Ч, 
	      remedy����������������stream��, ��Ҫ����MESA_set_stream_opt(), opt=MSO_TCP_RST_REMEDY ���.
		
    return value:
		>= 0: success.
		-1  : error.
*/
int MESA_rst_tcp(struct streaminfo *stream, struct rst_tcp_para *paras, int para_len);



/*
	���������ܵ�MESA_kill_xxxϵ�к���.
	���ӹ���Ϊ: 
	    ��ʵ�ʷ��͵����ݰ�copy��feedback_buf�ռ���, ������feedback_buf_lenΪʵ�����ݰ�����.

	ע��: feedback_buf_lenΪ���봫����, �����ʾfeedback_buf����, ������ʾʵ�ʷ��͵����ݰ�����.

    return value:
		>= 0: success.
		-1  : error.	
		-2  : feedback_buf or feedback_buf_len error.
*/
int MESA_kill_tcp_feedback(struct streaminfo *stream, const void *raw_pkt, char *feedback_buf, int *feedback_buf_len);
int MESA_kill_tcp_synack_feedback(struct streaminfo *stream, const void *raw_pkt, char *feedback_buf, int *feedback_buf_len);
int MESA_kill_connection_feedback(struct streaminfo *stream, const void *raw_pkt, char *feedback_buf, int *feedback_buf_len);

/* 
	CHN : ����route_dir����, Ϊ�˼���papp;
	ENG : compat for papp, dir reverse.
 */
unsigned char MESA_dir_reverse(unsigned char raw_route_dir);

/*************************************************************************************** 	
	NOTE:
		�ڱ���ģʽ��, ����������route_dir�ľ���ֵ, ֻ��Ҫ����ͬ��ͷ��򼴿�,
		��������������Ҫ��ȷ����route_dir��0����1, �������������ģʽ��ͬ, ������ʱ��仯,
		�������ô˽ӿ�, ���ֻ�贫����������ķ���, ���ص�ǰ��·��link route dir,
		ע��etc/sapp.toml inbound_route_dirҪ������ȷ.

	args: ��ʾ����Ŀ������ڵ�ǰ�豸���ڵĵ���λ��,
		'E' or 'e': ��ʾ���������Ǵ�Internal to External.
		'I' or 'i': ��ʾ���������Ǵ�External to Internal.

	return value:
		0 or 1: success.
		-1 : error.
****************************************************************************************/
int MESA_dir_human_to_link(int human_dir);

/*
	ARG:
		stream: ���ṹ��ָ��;
		payload: Ҫ���͵�����ָ��;
		payload_len: Ҫ���͵����ݸ��س���;
		raw_pkt: ԭʼ��ָ��;
		snd_routedir: Ҫ�������ݵ�route����, 
			 ��������͵İ��뵱ǰ��ͬ��, snd_routedir = stream->routedir, 
			 ��������͵İ��뵱ǰ������, snd_routedir = MESA_dir_reverse(stream->routedir).
	return value:
		-1: error.
		>0: ���͵����ݰ�ʵ���ܳ���(payload_len + �ײ��ͷ����);
*/
int MESA_inject_pkt(struct streaminfo *stream, const char *payload, int payload_len, const void *raw_pkt, UCHAR snd_routedir);


/*
	���������ܵ�MESA_inject_pkt_feedback����, ����ͬMESA_inject_pkt().
	��ʵ�ʷ��͵����ݰ�copy��feedback_buf�ռ���, ������feedback_buf_lenΪʵ�����ݰ�����.

	ע��: feedback_buf_lenΪ���봫����, �����ʾfeedback_buf����, ������ʾʵ�ʷ��͵����ݰ�����.

    return value:
		>= 0: success.
		-1  : error.	
		-2  : feedback_buf or feedback_buf_len error.
*/
int MESA_inject_pkt_feedback(struct streaminfo *stream, const char *payload, int payload_len, 
						const void *ext_raw_pkt, UCHAR snd_routedir,
						char *feedback_buf, int *feedback_buf_len);
						
int MESA_sendpacket_ethlayer(int thread_index,const char *buf, int buf_len, unsigned int target_id);//papp online, shuihu

/* �����ѹ���õ�����IP��, У��͵Ⱦ�������߼��� */
int MESA_sendpacket_iplayer(int thread_index,const char *buf,  int buf_len, __uint8_t dir);

/* �����ѹ���õ�����IPv4��, ����vxlan����, options��������vxlan��ص�ѡ�� */
int MESA_sendpacket_iplayer_options(int thread_index,const char *data,  int data_len, u_int8_t dir, SAPP_TLV_T *options, int opt_num);

/* �����ѹ���õ�����IPv6��, У��͵Ⱦ�������߼���, ����vxlan����, options��������vxlan��ص�ѡ�� */
int MESA_sendpacket_ipv6_layer_options(int thread_index,const char *data,  int data_len, u_int8_t dir, SAPP_TLV_T *options, int opt_num);
/* ����ָ������IP��, ��ָ����������, У�����ƽ̨�Զ�����,
   sip, dip������. */
int MESA_fakepacket_send_ipv4(int thread_index,__uint8_t ttl,__uint8_t protocol,
							u_int32_t sip_host_order, u_int32_t dip_host_order, 
							const char *payload, int payload_len,__uint8_t dir);

int MESA_fakepacket_send_ipv4_options(const struct streaminfo *stream, uint8_t protocol,
							uint32_t sip_host_order, uint32_t dip_host_order, 
							const char *payload, int payload_len, uint8_t dir, 
							SAPP_TLV_T *options, int opt_num);
							
int MESA_fakepacket_send_ipv4_detail(int thread_index,u_int8_t ttl,
							u_int8_t protocol,u_int32_t sip, u_int32_t dip, u_int16_t ipid, 
							const char *payload, int payload_len,u_int8_t dir);

int MESA_fakepacket_send_ipv6_options(const struct streaminfo *stream, uint8_t protocol,
							struct in6_addr *sip, struct in6_addr *dip,
							const char *payload, int payload_len, uint8_t dir, 
							SAPP_TLV_T *options, int opt_num);
/* ����ָ������TCP��, ��ָ����������, У�����ƽ̨�Զ�����,
   sip, dip,sport,dport,sseq,sack��������. */
int MESA_fakepacket_send_tcp(int thread_index,u_int sip_host_order,u_int dip_host_order,
							u_short sport_host_order,u_short dport_host_order,
							u_int sseq_host_order,u_int sack_host_order,
							u_char control,const char* payload,int payload_len, u_int8_t dir);

int MESA_fakepacket_send_tcp_detail(int thread_index,u_int sip_host_order,u_int dip_host_order,
										u_short ipid, u_char ip_ttl,
							u_short sport_host_order,u_short dport_host_order,
							u_int sseq_host_order,u_int sack_host_order, 
							u_char control, u_short tcp_win, const char* payload,int payload_len, u_int8_t dir);
int MESA_fakepacket_send_tcp_options(const struct streaminfo *stream,
							u_int sip_host_order,u_int dip_host_order,
							u_short sport_host_order,u_short dport_host_order,
							u_int sseq_host_order,u_int sack_host_order,
							u_char control,
							const char* payload,int payload_len, u_int8_t dir,
							SAPP_TLV_T *options, int opt_num);	
int MESA_fakepacket_send_ipv6_tcp_options(const struct streaminfo *stream,
							struct in6_addr *sip, struct in6_addr *dip,
							u_short sport_host_order,u_short dport_host_order,
							u_int sseq_host_order,u_int sack_host_order,
							u_char control,
							const char* payload,int payload_len, u_int8_t dir,
							SAPP_TLV_T *options, int opt_num);							
/* ����ָ������UDP��, ��ָ����������, У�����ƽ̨�Զ�����,
   sip, dip,sport,dport��������. */
int MESA_fakepacket_send_udp(int thread_index, u_int sip_host_order, u_int dip_host_order, 
							u_short sport_host_order,u_short dport_host_order, 
							const char *payload, int payload_len,u_int8_t dir);
							
int MESA_fakepacket_send_udp_detail(int thread_index, u_int sip_host_order, u_int dip_host_order, 
							u_short ipid, u_int8_t ip_ttl, u_short sport_host_order,u_short dport_host_order, 
							const char *payload, int payload_len,u_int8_t dir);
int MESA_fakepacket_send_udp_options(const struct streaminfo *stream,
							u_int sip_host_order, u_int dip_host_order, 
							u_short sport_host_order,u_short dport_host_order, 
							const char *payload, int payload_len,u_int8_t dir,
							SAPP_TLV_T *options, int opt_num);							
int MESA_fakepacket_send_ipv6_udp_options(const struct streaminfo *stream,
							struct in6_addr *sip, struct in6_addr *dip,
							u_short sport_host_order,u_short dport_host_order, 
							const char *payload, int payload_len,u_int8_t dir,
							SAPP_TLV_T *options, int opt_num);							
/* 
   ת��/���͵�ǰ���������ݰ�, 
   target_id: ����ָ��ת��/����Ŀ��, �������ļ�conf->send_raw_pkt.confָ������Ŀ���������豸��.
*/
int sapp_forward_current_pkt(const struct streaminfo *stream, unsigned int target_id);

enum sapp_send_pkt_opt_type{
	SAPP_SEND_OPT_IP_ID			= 0x10,			
	SAPP_SEND_OPT_IP_TTL		= 0x11,
	
	SAPP_SEND_OPT_TCP_WIN		= 0x20,
	
	SAPP_SEND_OPT_GDEV_DMAC		= 0x1101, /* GDEV-DMAC, �������������DMAC */
	SAPP_SEND_OPT_GDEV_SMAC		= 0x1102, /* local-SMAC, �������������SMAC */
	SAPP_SEND_OPT_GDEV_DIP		= 0x1103, /* GDEV-DIP, network order */
	SAPP_SEND_OPT_GDEV_SIP		= 0x1104, /* local-SIP, network order */
	SAPP_SEND_OPT_GDEV_UDP_DPORT=0x1105, /* GDEV udp dst port, network order */
	SAPP_SEND_OPT_GDEV_UDP_SPORT= 0x1106, /* local udp src port, network order */
	SAPP_SEND_OPT_VXLAN_FLAGS	= 0x1201, /* vxlan ��־λ */
	SAPP_SEND_OPT_VXLAN_VPN_ID	= 0x1202, /* vxlan vlan_id/vpn_id */
	SAPP_SEND_OPT_VXLAN_LINK_ID	= 0x1203, /* vxlan ��·id */
	SAPP_SEND_OPT_VXLAN_LINK_ENCAP_TYPE = 0x1204,  /* vxlanԭʼ�����װ��ʽ */
	SAPP_SEND_OPT_VXLAN_ONLINE_TEST_FLAG = 0x1205, /* vxlan���߲���λ */
	SAPP_SEND_OPT_VXLAN_LINK_DIR = 0x1206, /* vxlan��·����λ */
	SAPP_SEND_OPT_INNER_LINK_ENCAP_TYPE = 1301,  /* �ڲ�����װ��ʽ */
	SAPP_SEND_OPT_INNER_SMAC	= 0x1302, /* �ڲ�ԴMAC */
	SAPP_SEND_OPT_INNER_DMAC	= 0x1303, /* �ڲ�Ŀ��MAC */
	SAPP_SEND_OPT_INNER_VLANID	= 0x1304, /* �ڲ������VLAN������ */
	SAPP_SEND_OPT_VIRTUAL_LINK_ID = 0x1305, /* ����������·�ţ� ͬʱ��Ҫ��TUNNAT_CZ_ACTION_ENCAP_VIRTUAL_LINK_ID*/
	SAPP_SEND_OPT_REHASH_INDEX 	= 0x1306, /*����rehash index�� ͬʱ��Ҫ��TUNNAT_CZ_ACTION_ENCAP_VIRTUAL_LINK_ID*/
};

int MESA_fakepacket_send_ipv4_options(const struct streaminfo *stream, uint8_t protocol,
							uint32_t sip_host_order, uint32_t dip_host_order, 
							const char *payload, int payload_len, uint8_t dir, 
							SAPP_TLV_T *options, int opt_num);

int MESA_fakepacket_send_tcp_options(const struct streaminfo *stream,
							u_int sip_host_order,u_int dip_host_order,
							u_short sport_host_order,u_short dport_host_order,
							u_int sseq_host_order,u_int sack_host_order,
							u_char control,
							const char* payload,int payload_len, u_int8_t dir,
							SAPP_TLV_T *options, int opt_num);

int MESA_fakepacket_send_udp_options(const struct streaminfo *stream,
							u_int sip_host_order, u_int dip_host_order, 
							u_short sport_host_order,u_short dport_host_order, 
							const char *payload, int payload_len,u_int8_t dir,
							SAPP_TLV_T *options, int opt_num);
							
#ifdef __cplusplus
}
#endif

#endif